the definitive Xbase for web applications

Script Interpreter


MaxScript is the core of DBFree: the name identifies the interpreter (the engine) as well as the xBase-like language it implements.
MaxScript instructions works only inside web pages on a system that is MaxScript-enabled (of whom DBFree is the most widely used). If the interpreter is not installed the instructions in the page will have no effect. To make these instructions work they must be embedded into standard HTML using serve side tags (some tags that HTML reserves for this purpose) and the file extension of the file containing the page must be changed from .htm to .msp.
The server-side tags to be used with MaxScript are the same of ASP:

Anything inside these tags will be passed to MaxScript that will process the instructions and return the results in that same point of the page.
Results can be: One thing to remeber is that MaxScript can only work on server: remote users hardware is out of reach, and all interactions happens with them only throught the browser.
At the same time all the MaxScript code of the page to send to users is invisible to them because, in facts, is completely removed from the final pages sent to them, and can be seen only by someone physically sitting at the server console (assuming it has full access to system).

Learning MaxScript

Learning MaxScript is considerably easier than any other web scripting language around, and once understood the basics anyone can become productive in short time and start building basic web application.
What probably will take longer will be to learn the tricks of HTML for page beautifying and visual effects, something not indispensable (just look at Google interface) that you may postpose to a later time while concentrating into learning how to manage application and data logic.

The basic concept of MaxScript is to embed server-side scripts into HTML.
But how do the webserver distinguish between standard text and MaxScript statements?
This is done by using scripting blocks:


Everything contained into a scripting block is ignored by the webserver and passed to MaxScript for interpretation.
MaxScript considers code anything starting on a new line and ending with the next line. Next line is marked by your text editor using two non printable characters: carrage-return and line-feed (called hard carriage return). You won't see them but all the text following them will be displayed on  a new line.


The code written on a line is known as statement. All statements must consist in valid MaxScript code.

This example contains 3 statements (or lines of code):

PRINT "Welcome on this server:"
PRINT date()
PRINT time()
Like this other one:
<% dToday = date()
dTomorrow = dToday + 1
PRINT "Tomorrow will be " | dTomorrow %>

Statements may contain only commands, functions, comments, costants and variables.


This statement contains a variable:
x = 1

This other contains an expression:

x = (13 + 5) * 2 + (2/4)


This one contains a function:

x = timestamp())
DBFree and its various libraries offer hundreds of pre-defined functions. Furthermore you can write your own (but you cannot pack them into a custom library - unless if you're a registered developer). Functions you write are called UDFS (User Defined Functions) through all the documentation.


This other contains a command:

Commands are derived directly from xBase language: DBFree supports tens of commands of the standard (albeit all those that still have sense in the web environment). For a complete listing of xBase commands visti web site.


You can test the code of the examples by using the Command Prompt (a.k.a Command Window) that can be installed or is already provided with latest versions of DBFree, but for a working application you will need web pages.
Your webserver will pass to MaxScript only web pages with .msp extension, so all the active pages of your web (i.e. your application) needs to be renamed in this way.
In practice all static pages will retain the .HTML extension, while the active pages (often called scripts) will have extension .MSP

A  typical .msp file should contain at least the basic HTML tags, just like any other HTML file, plus MaxScript server-side code.
Below, we have an example of a simple script which sends the text "Hello World" to the browser:

print "Hello world! This is MaxScript"

Each code line in MaxScript must end with a carriage return. The next line distinguish one set of instructions from another.

Delimiters can be place anywhere in the page:

  <html><body><h3>This is the page <small><% PRINT pageName() %></h3>
  <p>You're connected from IP <%=ipAddr()%> at <%=time()%> GMT+1</p>

Notice the abbreviation <%=%> (called inline statement)  which is a common method to quickly insert results of functions in the middle of HTML tags.

There are two basic statements to output text with MaxScript: print on a new line (PRINT, usually abbreviated with ? ) and print on the same line (always abbreviated with  ??).

 In the example below we use the alternative sintax for print command to output the text "Hello World".

? "Hello World!" 
?? "Today is " 
?? date() 

Commenting the code

You can insert comments in the MaxScript code. The comments will be only visible when you open the source files for editing and will never reach the users that view the page on browser.
//-- this is a line of comment (all letters, numbers and punctuactions allowed)
//   this line is also commented
* this is another way to start a comment line (must be at the start of the line and cannot contain commas!)

Using the headers

Headers are fundamentals for DBFree: they define the behavior of your MaxPage as well as performing many other preparatory tasks .
Without an appropriate header your MaxPage won't work as it should (or at all).
Max Headers are always provided
At the beginning of the page, just below the <!DOCTYPE> tag you should insert the DBFree header
**************************************** CUSTOM HEADER
xcLib := mspconfigvar("LIBDIR")+"\free.lib"

set maxlib to &xcLib
***************************************** END

The header's purpose is to establish the DBFree common environment (setting paths and variables, etc) and loading the default libraries. Additional libraries can be specified as well. The mechanism is similar to that of Clipper's #include.

If your application is not intended to be portable you can also reference the necessary libraries "hard-coding" their paths in the page, like in the next example.

**************************************** LAZY HEADER
set maxlib to "d:\libs\dbx-4-0-1"
***************************************** END

Top of page