We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15954
    • 5 Posts
    I have about 5 pages in a new Evolution 1.0.2 site that need custom Javascript in the head section of the page. Each page will have it’s own unique Javascript code. I cannot use a {{chunk}} because it can only be placed in the content section ... right? How do I add the Javascript? Surely there’s a way. There’s gotta be.

    I don’t see how a TV will work since it is dependent on user input.

    Thanks in advance for any advice!!
      I just LOVE MODx! It's the greatest thing since the iPhone and the micro computer!
      • 26016
      • 561 Posts
      Hi, and welcome!
      You’re generally on the right track with a chunk. You can usually use a chunk near your content tag if you want. If your Javascript is not in the HEAD, your page may not validate; I’ve been lax about this a few times... don’t tell anyone! You’d know better than I, as it’s your Javascript, but I would think that it would generally work whether in the HEAD or around the content.

      The only problem with a chunk is lack of flexibility if you have a lot of different Javascripts you’ll need.

      Yes, users filling in TV’s for this, probably asking for it, although you can set up user groups to prevent that user from seeing the TV if you want. If your client is not stupid, maybe they can be asked not to touch that funny code in that one spot (yeah, right!). I don’t typically do this, but if you edit your TV, at the bottom you’ll see Access Permissions... if you’ve already set up, say, a user group that consists of only you (the Super Admin, so to speak), then you can check that box on Access Permissions.

      Here’s a random thought... if your script is almost the same on every page, you could use that block of Javascript in your template and use TV’s for the variables that differ per page. A little less fidding when editing the pages.

      Good luck, Dave

        MODx and Wordpress development
        Linux, PHP 5.2, MySQL 5.0, Evo 1.05, Revo 2.08-pl, Firefox 4
        • 7455
        • 2,204 Posts
        you could use a tv for that and make the tv only accessible for admin user.

        or make a snippet and depending on the id put a javascript in the head by using http://wiki.modxcms.com/index.php/API:regClientStartupScript

        a simple snippet would be like this:

        <?php
        
        $currentId = $modx->documentIdentifer();
        
        switch ($currentId ) {
            case 1:
                $src = "assets/js/first.js";
                modx->regClientStartupScript($src);
                break;
            case 15:
                $src = "assets/js/2nd.js";
                $modx->regClientStartupScript($src);        
                break;
            case 22:
                $src = "assets/js/last.js";
                $modx->regClientStartupScript($src);
                break;
        }
        return;
        

        where each case number is an id for the pages you like thejs to be added to the HEAD
        just put the snippet anywhere in the template. (on top would be good)

        hope this help

        Dimmy
          follow me on twitter: @dimmy01
          • 4041
          • 788 Posts
          A snippet would work in this situation I believe.
          Create a new snippet with the code below and call it in the <head> section of your template.
          Of course you would need to customize it to meet your specific requirements.

          <?php
          /*   custom_js_include snippet
          
               [!custom_js_include!]
          */
          
          $docid = $modx->documentIdentifier;
          
          $js_include_1 ='
          <!-- js_include_1 -->
          <!--[if lt IE 7]>
          <script type="text/javascript" src="assets/templates/test1/jquery/jquery.js"></script>
          <script type="text/javascript" src="assets/templates/test1jquery/jquery.dropdown.js"></script>
          <![endif]-->
          <!-- /js_include_1 -->
          ';
          
          $js_include_2 ='';
          $js_include_3 ='';
          $js_include_4 ='';
          $js_include_5 ='';
          
          switch($docid){
              case '1': $modx->regClientStartupScript($js_include_1);
              break;
          
              case '2': $modx->regClientStartupScript($js_include_2);
              break;
              
              case '3': $modx->regClientStartupScript($js_include_3);
              break;
              
              case '4': $modx->regClientStartupScript($js_include_4);
              break;
          
              case '5': $modx->regClientStartupScript($js_include_5);
              break;
              
              default: '';
              return;
          }
          ?>
            xforum
            http://frsbuilders.net (under construction) forum for evolution
            • 7455
            • 2,204 Posts
            nice 2 minds same solution hehe
              follow me on twitter: @dimmy01
              • 15954
              • 5 Posts
              Wow! You all totally ROCK! You guys are Royally Cool and then some!!!

              This is just what the doctor ordered! Thanks so much!!! laugh
                I just LOVE MODx! It&#39;s the greatest thing since the iPhone and the micro computer!
                • 28554
                • 201 Posts
                Here’s a thought if you want to be able to set it on a per-page basis:

                - Add a TV called jsLibraries, make it a multi-select and set the options sort of like so:

                jQuery=`jquery.js`||protoType=`prototype.js`||formValidation=`validation.js`
                


                - Add a chunk called dittoLibraries, and put the following in:

                <script type="text/javascript" src="js/[+jsLibraries+]"></script>
                


                - In your template call Ditto:

                [!Ditto&tpl=`dittoLibraries`!]
                


                Don’t forget to set it so that it won’t output "No documents found", and test it out smiley This is just the basic idea...

                On a side-note, I’m trying to start a trend by putting [SOLVED] in front of the thread-title (modify your first post) to clean up a bit, feel free to join smiley (no pressure).
                  • 36404
                  • 307 Posts
                  hi,

                  just a word for samb about js code that prevents pages from validating, it should not smiley is so, just put you code into a cdata, the page will validate... the problem being probably some &, >, < and so on here and there in the js code that, of corse, you can’t html encode js smiley

                  have swing
                    réfléchir avant d'agir