Fork me on GitHub

Generating and calling javascript code


Once the PHP objects are registered, the next step is to generate the corresponding javascript code. Jaxon provides 3 different functions for this purpose.

  • The function $jaxon->getCss() returns the CSS code to insert into the page. This will often be instructions to load CSS files of Jaxon plugins.
  • The function $jaxon->getJs() returns the code to load external javascript files.
  • The function $jaxon->getScript() returns the javascript code corresponding to registered functions and classes, and the configuration of the library.

The separation into three distinct functions makes it possible to insert the codes generated by the Jaxon library in different parts of the HTML page. The call to $jaxon->getScript(true, true) returns the cumulative output of the 3 previous calls.

The Javascript code

When the js.app.extern option is set to false, the call to $jaxon->getScript() returns the javascript code, which then will be directly included into the HTML code of the page.

If this option is set to true, the javascript code is saved into a file in the directory indicated by the js.app.dir option, and the call to $jaxon->getScript() returns the HTML code to load this file from the URL indicated by the js.app.uri option. If in addition, the js.app.minify option is set to true, the javascript code is minified and the file name ends with .min.js.

The javascript file will not be regenerated if it already exists. It is then recommended to set the js.app.extern option to false when developping, and generate a minified javascript file when deploying the application.

Javascript calls

When a PHP class is registered with Jaxon, the javascript class name is the name of the PHP class prepended with the value of the core.prefix.class configuration option. The default value of this option is Jaxon.

$jaxon->register(Jaxon::CALLABLE_OBJECT, new HelloWorld());
<button onclick="JaxonHelloWorld.sayHello(0)">Click Me</button>

When a PHP function is registered with Jaxon, the javascript function name is the name of the PHP function prepended with the value of the core.prefix.function configuration option. The default value of this option is jaxon_.

$jaxon->register(Jaxon::USER_FUNCTION, 'helloWorld');
<button onclick="jaxon_helloWorld(0)">Click Me</button>

The parameters of javascript calls

Parameters of any type can be passed to the javascript functions generated by Jaxon: integer, boolean, string, array or object. They are automatically passed to the corresponding PHP classes.

The jaxon.getFormValues(id) function reads the content of the HTML form with a given id.

In this example the content of a HTML form is passed as parameter to the function.

<button onclick="JaxonHelloWorld.sayHello(jaxon.getFormValues('DemoForm'))">Click Me</button>

The jaxon.$(id) function reads the content of the HTML element with a given id.

In this example the value of a text field is passed as parameter to the function.

<button onclick="JaxonHelloWorld.sayHello(jaxon.$('DemoData').value)">Click Me</button>

In this example the value of a checkbox is passed as parameter to the function.

<button onclick="JaxonHelloWorld.sayHello(jaxon.$('DemoData').checked)">Click Me</button>

In this example the content of a HTML element is passed as parameter to the function.

<button onclick="JaxonHelloWorld.sayHello(jaxon.$('DemoDiv').innerHTML)">Click Me</button>

The Request Factory provides functions to generate the above javascript calls as well as their parameters from PHP.