We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6705
    • 79 Posts
    I want to include a small amount of JavaScript code in a particular snippet-powered page, and was pleased to discover that there is a MODx function (regClientStartupScript (the link is to the Revo version of the documentation, as I couldn’t find the corresponding page for Evo)) which you can use in a snippet which will let you insert a <script> element into the document <head> (because <script> within the <body> is apparently not permitted in XHTML 1.0, and I’d like to stay valid).

    So far, so good, but I need to include " onload=myFunction()" in the <body> tag in order to trigger my code (or possibly I need to rewrite my JavaScript code so that this is not necessary, but I’m no real JavaScript expert. What my code needs to do is set the ’display’ attribute of a particular form input field when the page loads, based on the value of a particular radio button elsewhere in the form. The only ways that I’m aware of being able to do this are either via onload, or via an in-<body> script just after the input field in question).

    Is there a similar MODx function that would allow me to tweak the body tag from within a snippet in this way?

    The only other alternative I can think of would be to add a Template Variable in the template for this page, but I really don’t want to do this, as the page template is the same as used by most pages on the site and I don’t want to make such a TV available to other users or have to create a new template. (If there’s a way to make a TV available to a template, but only when a page is part of a particular Resource Group, then perhaps that might be a possibility?)

    Thanks for any advice!
      Please don't PM me unless it's absolutely essential: if a technical question is worth asking, it's worth asking in public, so that others can share their experience, and so that all can learn from the answers.
      • 4041
      • 788 Posts
      In your template, modify your body tag like so:
      <body[+body_extra+]>


      Then in the code of your snippet, set the placeholder:
      $modx->setPlaceholder('[+body_extra+]', ' onload=myFunction()');


      On the pages which do not have the placeholder set to a value, modx will just silently remove the empty placeholder.

      Hope that helps smiley
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 6705
        • 79 Posts
        Many thanks, that’s exactly what I needed! smiley

        (And I can see other potential uses for setPlaceholder too..)
          Please don't PM me unless it's absolutely essential: if a technical question is worth asking, it's worth asking in public, so that others can share their experience, and so that all can learn from the answers.