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
    Several different varieties of pie at that wink
      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
      • 19872
      • 1,078 Posts
      Hey Susan: If I can tap your assistance with one more detail. Now I am trying to apply the concept to put a script in the head, and the onload aspect is stumping me. If it were a js file the example in modx docs would be easy to follow, but how to I write to get my variable to appear onload?

      $addAi ='<script>your javascript code</script>';
      $modx->regClientStartupScript($adAi);

      or maybe the class itself takes care of the onload aspect and it's the example given in the docs that is confusing to me.
      $modx->regClientStartupScript('assets/js/onload.js');
      • discuss.answer
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        If you want a code block inserted, then your variable has to be a full code block, including the enclosing script tags.

        For example, if you do this
        $js = "alert('active');";
        $modx->regClientStartupScript($js);
        

        You will get this
        <script type="text/javascript" src="alert('active');"></script>
        </head>

        If you do this
        $js = "<script>
        alert('active');
        </script>";
        $modx->regClientStartupScript($js);

        then you'll get
        <script>
        alert('active');
        </script>
        </head>
          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
          • 19872
          • 1,078 Posts
          I wrote this before you had responded.

          $addAi ='<script>document.cookie=\'resolution=\'+Math.max(screen.width,screen.height)+\'; path=/\';</script>';
          $modx->regClientStartupScript($adAi);

          The references to alert and active are confusing to me. I basically did the above in my first site, only using regClientScript and it seems to work.
            • 19872
            • 1,078 Posts
            Or is alert & active just example of code, not necessarily indicative of an action related to the snippet?
              • 19872
              • 1,078 Posts
              Mine wasn't working. Amazing how just one thing trips the whole thing up. adAi works better if it has an extra "d" - - - addAi.

              So this works very nice. Let's say I want this to be the very tip top first thing to load in the head. Is there a way to control it's position in that regard?
                • 19872
                • 1,078 Posts
                Mine wasn't working. Amazing how just one thing trips the whole thing up. adAi works better if it has an extra "d" - - - addAi.

                So this works very nice. Let's say I want this to be the very tip top first thing to load in the head. Is there a way to control it's position in that regard?

                So my big, full screen home page slider is a MIGx TV. With all you have enlightened me to, I can load the js for the slider, the script for Adaptive Images (which detects device window size and determines which size image to source) and have it all load together. This is really really cool. I see this as a way to cut down on the number of templates I might need.

                I can't thank you enough for passing this along to me.