API:regClientStartupScript

From MODx Wiki

Jump to: navigation, search
 API Function Definition: 
regClientStartupScript
Versions 
> 0.9.2 ?
Return Values
Success: unknown
Fail: unknown
Data Type 
string
Object Hierarchy 
DocumentParser
string regClientStartupScript(string $src);


Registers Startup Client-side JavaScript - these scripts are loaded inside the <head> tag.

This function is useful for including javascript code that is used by the snippet. By using this function in a snippet you avoid the necessity of including the javascript in the site template, whether other pages use the script or not. Only the pages that use the snippet will have the javascript code.

Example 1

$src = "assets/js/prototype.js";
$modx->regClientStartupScript($src);

This will produce this line of code in the head of the document:

<script type="text/javascript" url="assets/js/prototype.js"></script>


Example 2

You can also pass a complete block of javascript code:

$src2 = "<script type='text/javascript'>
  function getHTML()
  {
    var url = 'testing.php';
    var pars = 'return=test';
    var myAjax = new Ajax.Updater( {success: 'placeholder'}, url, {method: 'get', parameters: pars});
}
</script>";
 
$modx->regClientStartupScript($src2);

The entire block of text will be inserted into the head of the document.

Related functions

API:regClientScript

Personal tools