We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    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.
      Studying MODX in the desert - http://sottwell.com
      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
      Join the Slack Community - http://modx.org
      • 34660
      • 6 Posts
      Ah ok so if i put for example

      <?php
      $calscript = "<script type=\"text/javascript\">
      SCRIPT HERE
      </script>";
      return;
      ?>
      


      as a snippet then that would work just the same ?
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        No. If you look at the code I’ve posted the variable assigned to the block of javascript code is passed to the regClientStartupScript() function.

        If you just want to put something in the template with a snippet without using the API function, you need to have something like this:
        $calscript = "<script type=\"text/javascript\">
        SCRIPT HERE
        </script>";
        return $calscript;

        Think of a snippet as a function. If it doesn’t echo, print or return something (which gets echo’ed or printed later by the calling process) then nothing will get displayed. The variable will be assigned, but it won’t do anything. The various regClient...() API functions take their arguments and insert them into the HTML document either in the head (in the case of regClientStartup...() functions) or at the bottom of the HTML document (in the case of the regClient...() functions). If the argument isn’t a fully formed <script>...</script> or <style>...</style> block then MODx automatically makes it into a <link ... /> insertion using the function’s argument as the URL for the link and assigning the proper type= attribute according to which function was used.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 33968
          • 863 Posts
          I think the original request was just how to include a script on a page, rather than how to do it dynamically. That’s some nice code for loading only the scripts you need though smiley

          It’s as easy as opening up your template and pasting your script straight in there, just as you would if you were coding a static html page.
          <script>
          SCRIPT HERE
          </script>
          

          Or for an external script:
          <script src="external/script/path/script.js"></script>
          


          However if you’ve attempted the tutorials as suggested I don’t think you’d still be asking this question wink
          This will get you going: http://codingpad.maryspad.com/2010/08/18/modx-revolution-for-beginners-part-1-introduction/

          Cheers!