We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25551 ☆ A M B ☆
    • 1,231 Posts
    Hey, I want to call a javascript only on certain pages and my guess is to use TV.

    I want to call <script type="text/javascript" src="assets/js/myscript.js"></script>

    I’ve tried setting it up but it’s not going to plan. I managed to make check boxes but where do i put the script call exactly?

    Thanks.
      Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
      AugmentBLU - MODX Partner

      BLUcart - MODX Revolution E-Commerce & Shopping Cart
      • 7231
      • 4,205 Posts
      You can build a snippet and use the API function regClientScript to add the js at the end of the page (like google analytics) or regClientStartupScript to load it in the head section.

      http://wiki.modxcms.com/index.php/API:regClientStartupScript
      http://wiki.modxcms.com/index.php/API:regClientScript

      And you could use a conditional to work with the checkboxes to know to load the 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]
        • 4639
        • 36 Posts
        A very simple way might be creating a text TV, taking care of adding the apropriate [*TVname*] in the template.

        Regarding the checkbox you can define the <script> tag in the Input option values: field when editing the TV, in the form: option1==value1||option2==value2||option3==value3 (see the documentation)

        For example:
        mootools==<script type="text/javascript" src="path/to/scripts/mootools.js"></script>||jquery==<script type="text/javascript" src="path/to/scripts/jquery.js"></script>


        Also you could use @ bindings instead of the actual value:
        mootools==@CHUNK chunk_name


        where chunk_name would be:
        <script type="text/javascript" src="path/to/scripts/mootools.js"></script>
          • 25551 ☆ A M B ☆
          • 1,231 Posts
          Hey, I know it’s a little long since I started this thread but I just got round to doing this properly and I can’t seem to quite get it to work.

          I have tried the following settings

          trial 1

          Variable Name: [*lightbox*]
          Input Type: checkbox
          Input Option Values: on==On||off==Off
          Default Value: lightbox==@CHUNK lightbox

          trial 2

          Variable Name: [*lightbox*]
          Input Type: text
          Input Option Values: lightbox==@CHUNK lightbox
          Default Value: (blank)

          The lightbox chunk has the script lines like below

          <script type="text/javascript" src="assets/js/shadowbox/js/yui-utilities.js"></script>
          




          I tried the 2 ways above... and i put [*lightbox*] just before my </head> tag and when i refresh the page i get 2 outcomes, neither of which work.

          On
          <script type="text/javascript">var MODX_MEDIA_PATH = "media";</script>
          
          </head>


          the work On in the above code actually appear at the very top of my website even thought it’s in the head tag and not the body...... very odd.

          I know i’m doing something wrong...... I read the wiki on TV but my mind isn’t functioning correctly, not that it ever does. rolleyes


            Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
            AugmentBLU - MODX Partner

            BLUcart - MODX Revolution E-Commerce & Shopping Cart
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            default value: @CHUNK lightbox

            Or you can just leave it empty, and in the pages where you want it edit the TV and put the chunk tags: {{chunkname}}

            I try to avoid having things with the same name, although it really doesn’t hurt anything, it can get confusing which lightbox you are dealing with, the TV or the chunk.
              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
              • 7231
              • 4,205 Posts
              The check box is tricky. I don’t think the way that you have it set up will work. Based on my experience the check box only has an on state, the off or default state does not registered in the database. For this to work you need to have a snippet to do a bit of logic like if there is a value do this if not do something else.

              Here is an untested idea off the top of my head:
              Create a snippet with:
              <?php
              $input = isset($input) ? $input : '';
              if ($input) {
              	$src = "assets/js/shadowbox/js/yui-utilities.js";
              	$modx->regClientStartupScript($src);
              }
              return '';	
              ?>

              and then place the call in the document (I will call JSLoad for example):
              [!JSLoad? &input=`[*checkbox*]`!]


              You can place this in the template or in the content field or wherever a snippet can be called.

              The result you descrube seem to be a conflict with quickedit, check the site when logged out of the manager. I usually keep a second browser open side by side with one logged in ans the second not (need two browsers not two browser windows).
                [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]
                • 25551 ☆ A M B ☆
                • 1,231 Posts
                I have been trying all sorts of options but the checkbox name appears in my website.

                I have a chunk called lightbox and the tv is called lightboxJS

                the lightbox chunk has the call to the scripts and the tv is setup with the checkbox option, Input Option Values: off==Off (also tried just Off which worked but still appears in the site content) , Default Value: @CHUNK lightbox

                In the head of the template i put [*lightbox*]

                I have 3 TV setup with all the same option so I can disable certain scripts from certain pages and the word Off is appearing 1-3 times, depending on the options checked

                and if I want the script on a specific page due to conflicts which I’m suffering from I click the checkbox to have it off... it does switch off but the word Off appear at the very top of my website.
                This happens in another browser with out being logged in as an admin... anyway i have quick edit module and the plugin disabled as I don’t use them.


                *** Just took a look at my souce code and the word Off is in the head section of the website.... there’s nothing in the body at all but it’s showing in the browser?

                I tested using IE 7 and FF on my vista machine.
                IE6 and Opera on a virtual XP install

                All show the same
                  Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                  AugmentBLU - MODX Partner

                  BLUcart - MODX Revolution E-Commerce & Shopping Cart
                  • 7804
                  • 80 Posts
                  Here’s how I did it:

                  1. Create the chunks; each one contains a call to a script that you will want to set on a page-by-page basis. For example {{mootools}}, {{googleanalytics}}, {{lightbox}}

                  (note: include a line break with each chunk so when calling multiple scripts they don’t all get jammed together on the same line)

                  2. Create the TV:

                  [*scriptInclude*]

                  input type = checkbox

                  input option values = Mootools=={{mootools}}||Google Analytics=={{googleanalytics}}||Lightbox=={{lightbox}}

                  Default Value: @CHUNK lightbox

                  3. Assign the TV to your template and make sure the TV is called in the template properly. It looks like your TV is not being called by its exact name.

                  You can’t use the checkbox as an on/off toggle; MODx will just include the value "off" in your template which is what you’re seeing.
                    • 4639
                    • 36 Posts
                    Just some graphics to illustrate what have been said before:
                    (see attachments)

                    • The chunk creation
                    • The template variable
                    • The template
                      • 25551 ☆ A M B ☆
                      • 1,231 Posts
                      Yeah that worked nicely. Thanks!
                        Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
                        AugmentBLU - MODX Partner

                        BLUcart - MODX Revolution E-Commerce & Shopping Cart