{ title: 'Embedded Javascript', crumbs: [ { "User's Guide": 'index.html' }, ], }

# Embedded Javascript

GoAhead provides an embedded version of Javascript for use in JST pages. As the full Javascript 6 specification is quite a large language, GoAhead's implements a small subset suitable for embedded use. GoAhead Javascript™ is a strict subset of Javascript and implements the essential elements of the language. It also provides the engine for JST pages to enable the easy creation of dynamic web pages.

A standalone product, Embedthis Ejscript™ more fully supports the latest Javascript 6 specification and when used with the Appweb embedded web server is an alternative to GoAhead if you have sufficient memory available.

When GoAhead Javascript is used inside a JST web page, it consists of a script within JST delimiters. The basic format is:

    <% function(arguments, ...); %>

Javascript functions are created by the jsSetGlobalFunction. When the function is evaluated, the corresponding C procedure that implements the Javascript function is called.

GoAhead Javascript implements the following Javascript elements:

- Case sensitivity
- White space
- Semicolons
- Comments
- Identifiers
- Data types including numbers, booleans, and strings with backslash characters
- Full expressions
- If/else, for, return
- Global function calls

The following language elements are not implemented:

- Arrays
- Objects
- Labeled statements
- Control flow statements including: break, case, continue, default, do/while, export, for/in, function, import,switch, var, while, and with
- Regular expressions

Javascript scripts can span multiple lines by using "\" as the last character on the preceding line. When used in JST pages, function arguments can contain any query variable defined in either the URL query string or the standard variable set. URL query strings are automatically decoded, and Javascript variables are defined to the decoded query value. For example, to parse the name and address encoded as a query string in a URL, use the following code:

    http://localhost/mypage?name=smith&age=35

    int myAspProcedure(Webs wp, int argc, char **argv) {
        char *name = websGetVar(wp, "name", "undefined");
        char *age = websGetVar(wp, "age", "undefined");
        websWrite(wp, "Name %s, Age %s", name, age);
    }

Javascript procedures are registered by using the websJstDefine API. This publishes a C procedure as a Javascript global function. For example:

    extern int outputMyTable(int ejid, Webs wp, int argc, char **argv);
    websJstDefine("outputTable", outputMyTable);

The websJstDefine call publishes the Javascript command "outputTable" and links it to the outputMyTable C procedure. When an JST page is requested by the user's browser, any JST Javascript which uses the outputTable command will cause the outputMyTable to be called with the relevant arguments.