We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23571
    • 223 Posts
    I am pretty new to Modx and after reading a lot of the documentation out there I have a "How to" question. I have a page with a long javaScript reference but it is only required on a single page, what is the best method for having this called in (turned on) inside of the <head> tag on only a single page?

    I have been looking at the template variables for this solution, but am not sure if that is the best approach or not (or even if that is possible). I realize I could create a second template for this to occur, but I believe that the Modx system is flexible enough to accomplish this with a more elegant solution if I could find someone that could point me in the right direction.
      • 25663 MODX Staff
      • 12,272 Posts
      1) Create a new TV and assign it to all your templates with a type of Textarea: [*extraHead*]
      2) Assign it a default value of "@INHERIT" (this part is optional)
      3) Put it in your head of your template where appropriate
      4) When you want extra JS or CSS in your document head for a particular page, just enter away

      Note about optional step 2:
      This makes it very easy to create an entire section having extra code (js, css, etc.) automatically inserted simply by adding it to [*extraHead*] for the parent container. All it’s children and below will inherit the value by default, unless manually and otherwise overridden.

        Ryan Thrash, MODX Co-Founder
        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
        • 23571
        • 223 Posts
        Thank you, thank you, thank you. I get it. Wow, I am beginning to appreciate how powerful these TV’s can be.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Another way to do this without having the TV field on all pages if you only want the js for one, is to make a snippet with the $modx->regClientStartupScript() function and put that snippet’s tags in that document’s content.
            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
            • 23571
            • 223 Posts
            So far the previous TV suggestion is working pretty well for my needs as I have actually been able to use the TV for multiple pages requiring different, page specific JavaScript calls.

            As I seem to be in major Modx learning mode I am very interested in exploring uses for the solution you suggested. Is this a snippet in the Resource Repository? I am not finding it in the site. Could you point me in the right direction?

            Thanks.
              • 7231
              • 4,205 Posts
              $modx->regClientStartupScript()
              THis is from the MODx API which provide functions to interact with the MODx framework. There is some info in the wiki and some info in the official modx documentation.

              It is not extensively documented but you can get a good idea of what is available. Another thing to do is to take apart existing snippets and see how they are built.
                [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                Something is happening here, but you don&#39;t know what it is.
                Do you, Mr. Jones? - [bob dylan]
                • 23571
                • 223 Posts
                Ah, found it. Thanks for the assistance.
                  • 27733
                  • 99 Posts
                  Taking this further, this reminds me of a concept I’ve been pondering for a project I’m currently working on; one where I’d like to have the appropriate script load, depending on the presence of a particular object with a particular class or id.

                  For example, I’m using a js script for a tree menu in a secondary navigation area (the list being output from Wayfinder). The tree menu(s) appear only in certain sections of the site, and it’d be nice to load the js only when, say, <div class="nav2"> is found. Similarly, another script is being used to "zebra" stripe tables, so it’d be great if one could insert a <table class="zebra"> for example, and the proper script would load based upon finding the class table.zebra.

                  Does this sound sensible, or over-complicated? And if it seems compelling, how easy (or not) is it to achieve?
                    ------
                    Server: *nix, ModX Evo 1.0.4, Apache 2.x, PHP 5.x, Mysql 5.x.
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    You can make a simple snippet to load the js files using the $modx->regClientStartupScript(), and put the snippet tags in the documents that use the menu or the table. This could be semi-automated to include groups of documents under the same folder in the Tree by using a TV with @INHERIT, then in the parent folder edit the TV to either call a chunk with the snippet, or use @EVAL to run the function in the TV (lots of ways to do things in MODx).

                    If you wanted this done automatically, you could make a plugin that adds the js where it finds those elements, but that would require considerable overhead on each and every page request to parse the entire document object again. It’s a trade-off between user responsibility and page generation time and resources.


                      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
                      • 27733
                      • 99 Posts
                      Thanks, Susan for your insights. I suspected that overhead may be an issue with that idea. Perhaps once I gain some plug-in writing skills, I’ll experiment with it a bit...
                        ------
                        Server: *nix, ModX Evo 1.0.4, Apache 2.x, PHP 5.x, Mysql 5.x.