We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5091
    • 332 Posts
    What is the best way to call up Javascript for specific page that is to be placed in the <head>?

    I have 5 different pages that need a bit of different javascript in the header, not the prototype, jquery links for external files but the load functions. I first created 5 chunks and use the getChunks snippet, but it doesn’t work because chunk is only good for html code.

    Was reading ’Creating Snippets’ article from the Wiki, I cannot go very far as my PHP knowledge is still at Kindergarten level

    ’http://wiki.modxcms.com/index.php/Creating_Snippets

    I really don’t want to create 7 templates if possible.

    Any guidance or pointer is greatly appreciated.
      • 5091
      • 332 Posts
      thank you so much for the quick response.

      Hmmm, now I think I must have done incorrectly with getChunks snippet. When it didn’t works, I read the "Placeholders used by MODx Pages and Templates" article and this ’Chunks are ’raw’ HTML code’ caught my eyes.

      Will try it again!
        • 7231
        • 4,205 Posts
        You can also use a snippet to load the JS in the head section of the page without adding any new TVs by using the regClientStartupScript() api function.

        [!jsLoad? &jsChunk=`chunkname`!]

        <?php
        // the only param is jsChunk=`chunkname`
        // jsChunk is the name of the chunk with the javascipt
        
        $jsChunk = isset($jsChunk) ? $modx->getChunk($jsChunk) : '';
        $modx->regClientStartupScript($jsChunk);
        return '';
        ?>


        This may be handy if you want to use the JS sporadically and not necessarily related to any specific template.
          [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]
          • 4310
          • 2,310 Posts
          To carry on from dev_cw.
          If you want to load different bits of JS depending on the page then a snippet like this;
          <?php
          global $modx;
          $output = '';
          $docid = ($modx->documentIdentifier);
          if ($docid == 39) {
          $output .= '{{chunkwithJS}}';
          }
          if ($docid == 100) {
          $output .= '
          <script type="text/javascript">
          $(100).ready(function() {
          $("table") .tablesorter({widthFixed: true, widgets: [\'zebra\']})
          .tablesorterPager({container: $("#pager")});
          });
          </script> ';
          }
          else
          $output .= '';
          return $output;
          ?>

          Lets you have the JS in chunks or directly in the snippet and vary it page by page.
          That’s what’s so good about MODx half a dozen ways to achieve the same thing.
            • 5091
            • 332 Posts
            Thank you bunk58 and dev_cw.

            The snippet should placed in the Resources IMHO smiley
              • 7231
              • 4,205 Posts
              I think there may already be one that does this. i will take a look and if nothing is available I will add (and possibly add some more features) this snippet.
                [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]
                • 5091
                • 332 Posts
                Thanks Dev_cw. I didn’t see anything similar in Resources, but maybe I missed it.

                One more question, what do I modify if I want, docid 10 and docid15 to use the same chunk?

                if ($docid == 10) {
                $output .= ’{{chunkwithJS}}’;
                }

                  • 34017
                  • 898 Posts
                  I’ve been thinking of adding a TV that only the super admin sees to place certain scripts styles for individual pages. I guess you could have it default to a chunk or snippet and add to or replace it.

                  That seems like the easiest for me.

                  Then if you need to know what pages have something specific, you could use tvExplorer to search.
                    Chuck the Trukk
                    ProWebscape.com :: Nashville-WebDesign.com
                    - - - - - - - -
                    What are TV's? Here's some info below.
                    http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                    http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
                    • 4310
                    • 2,310 Posts
                    Just repeat for each docid e.g.
                    if ($docid == 10) {
                    $output .= '{{chunkwithJS}}';
                    }
                    if ($docid == 15) {
                    $output .= '{{chunkwithJS}}';
                    }
                    if ($docid == 39) {
                    $output .= '{{anotherchunkwithJS}}';
                    }
                      • 5091
                      • 332 Posts
                      Thanks Bunk58 for super quick reply.

                      I thought about repeating it might work, but thought there maybe easier way and better practise to combine two docid in one - by asking question like this, I pickup new thing faster smiley