Here’s an example of a snippet I use to load Javascript into a page that needs it; this way pages that don’t need it aren’t cluttered up with unnecessary code or requests for unnecessary external files.
<?php
$modx->regClientStartupScript('manager/media/script/mootools/mootools.js');
$modx->regClientStartupScript('assets/js/calendar.js');
$calscript = "<script type=\"text/javascript\">
window.addEvent('domready', function() {
postDate = new Calendar({ datepost: 'm/d/Y' });
expDate = new Calendar ({ appexpiration: 'm/d/Y' });
});
</script>";
$modx->regClientStartupScript($calscript);
$modx->regClientCSS('assets/js/css/datepicker.css');
return;
?>
This particular snippet loads the javascript and css, both as links to files and as inline javascript, into the page’s HTML head. I just put the snippet’s tags in the content of the page that has the form where I want this stuff to appear.
It first inserts the links to the required library files, then inserts some inline javascript, the inserts the link to the css file needed by the javascript datepicker widget. The $modx->regClient... functions automatically determine if you’re registering inline code or they need to make it into a link.