We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1176
    • 8 Posts
    Hi,

    I’m hoping this is an obvious question, with an easy answer. I have one template for my site, but some pages require javascript for things like LightBox. do I have to include these on every page, or is there a way to have them only included in the page when needed ?

    Thanks,

    Allen
      • 4310
      • 2,310 Posts
      Have a look here
        • 22303 MODX Staff
        • 10,725 Posts
        Quote from: AllenConquest at May 29, 2008, 10:07 AM

        I’m hoping this is an obvious question, with an easy answer. I have one template for my site, but some pages require javascript for things like LightBox. do I have to include these on every page, or is there a way to have them only included in the page when needed ?
        Allen:

        I always create a TV and attach to all my templates in which I can stick custom HEAD content, like Javascripts needed for specific pages. Using @INHERIT, you can even change this for entire branches on your site taxonomy by editing a single document’s TV value.

        There are many other ways to do this in MODx, including using Snippets to register javascript in the HEAD programmatically. See the $modx->regClientStartupScript() function.
          • 1176
          • 8 Posts
          Thanks for your suggestions.

          I did some searching myself and came across the $mod->regClientStartupScript() and corresponding $mod->regClientCSS() functions. The way I now do this is to create a Snippet and use these function to load the required JS and CSS for the page. I can then add this snippet code to my page (created by my site template). This way I don’t need to hard-code any document IDs, or put any conditional logic in the page, I just add the Snippet to any page that needs it.

          I’m being to enjoy MODx and appreciate the power of the application.

          Allen