We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14050
    • 788 Posts
    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? &register=`jquery.js,custom.js,special.js`!]


    The parameter &register 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? &register=`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 '';
    ?>
      Jesse R.
      Consider trying something new and extraordinary.
      Illinois Wine

      Have you considered donating to MODx lately?
      Donate now. Every contribution helps.
      • 31037
      • 358 Posts
      Perfect timing!

      I’ll definitely use it! In about five minutes! laugh

      Thanks!
        • 14050
        • 788 Posts
        Glad to hear that it may be useful to someone else.
          Jesse R.
          Consider trying something new and extraordinary.
          Illinois Wine

          Have you considered donating to MODx lately?
          Donate now. Every contribution helps.