We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4097
    • 15 Posts
    I’m looking to load a javascript through the body tag, but only on certain pages. What is the best way to accomplish this? Here is the script:

    <script>
    // =======================================
    // set the following variables
    // =======================================

    // Set slideShowSpeed (milliseconds)
    var slideShowSpeed = 3000

    // Duration of crossfade (seconds)
    var crossFadeDuration = 5

    // Specify the image files
    var Pic = new Array() // don’t touch this
    // to add more images, just continue
    // the pattern, adding to the array below

    Pic[0] = ’banner1.jpg’
    Pic[1] = ’banner2.jpg’
    Pic[2] = ’banner3.jpg’
    Pic[3] = ’banner4.jpg’

    // =======================================
    // do not edit anything below this line
    // =======================================

    var t
    var j = 0
    var p = Pic.length

    var preLoad = new Array()
    for (i = 0; i < p; i++){
    preLoad[i] = new Image()
    preLoad[i].src = Pic[i]
    }

    function runSlideShow(){
    if (document.all){
    document.images.SlideShow.style.filter="blendTrans(duration=2)"
    document.images.SlideShow.style.filter="blendTrans(duration=crossFadeDuration)"
    document.images.SlideShow.filters.blendTrans.Apply()
    }
    document.images.SlideShow.src = preLoad[j].src
    if (document.all){
    document.images.SlideShow.filters.blendTrans.Play()
    }
    j = j + 1
    if (j > (p-1)) j=0
    t = setTimeout(’runSlideShow()’, slideShowSpeed)
    }
    </script>
      • 10487 MODX Staff
      • 1,535 Posts
      One option is to use a checkbox Template Variable. Create a new TV and set the input type to ’checkbox’ and have the input options set as follows:
      Include Script=={{script}}

      Now, create a chunk called ’script’ with your JS code as the content. Finally in your template, insert the Template Variable where you want the script to load, like this (obviously, change the name based on what you called your TV):
      [*myScriptTV*]


      With that all done, all you have to do is set the checkbox TV when editing a document to include the script or not. smiley
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com