Here is a little snippet that I created that I find myself using time and time again. Sometimes you only have a few pages that require the loading of javascript. For performance sake, you would not want to just use a global declaration in a template that loaded the javascript on all pages, including ones that have no need for the javascript library. So you have a couple of options. You can make the declaration in a separate template, and select that template when you need the javascript. You can also create a text input template variable and place it right before the </head> section in each of your templates. You could then manually enter in the <script> tags as necessary. Or you could use this snippet, which utilizes the API call "$modx->regClientStartupScript." Essentially, it allows you to make a snippet call to load as many javascript files as necessary for that individual page. An example snippet call that you could place in the document is:
[!RegisterJS? ®ister=`jquery.js,custom.js,special.js`!]
The parameter ®ister takes a comma delimited list of files and then runs them through the API call. If you do not include a directory location, it assumes you placed the files in "assets/js/." So you could also make a call like this:
[!RegisterJS? ®ister=`assets/myjsfiles/jquery.js,http://www.google.com/google.js,special.js`!]
I find it useful and I hope someone else out there might find it useful as well.
<?php
if (isset($register)) {
$files = explode(',',$register);
foreach ($files as $file) {
if (strpos($file,'/')) {
$modx->regClientStartupScript($file);
} else {
$modx->regClientStartupScript('assets/js/'.$file);
}
}
return '';
}
else return '';
?>