We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33706
    • 51 Posts
    Hi.

    I would like to keep the same template for the whole site;
    How can I include some javascript files only in certain documents; let say in document 21, 25 and 30 for example.

    thanks
      • 7923
      • 4,213 Posts
      You could do a TV that includes the javascripts.. For examply a checkbox TV that includes the scripts if the checkbox is checked, or just text field where you can write the javascript include. Or you could do a snippet what checks for document id and includes the scripts if needed..

      for example:
      $includeIds = (isset($includeIds)) ? explode(",",$includeIds) : array();
      $docId = $modx->documentIdentifier;
      if (array_search($docId, $includeIds)) {
        $strJavascript = "assets/js/prototype.js";
        // or
        $strJavascript = "<script type='text/javascript'>alert('some javascript');</script>";
        // or
        $strJavascript = $modx->getChunk("chunkname");
      
        $modx->regClientStartupScript($strJavascript);
      }
      return;
      


      That snippet would take the document ids like [[includeJsSnippet? &includeIds=`21,25,30`]]


        "He can have a lollipop any time he wants to. That's what it means to be a programmer."
        • 33706
        • 51 Posts
        Thanks a lot.

        This is the solution that worked for me :
        $includeIds = (isset($includeIds)) ? explode(",",$includeIds) : array();
        $docId = $modx->documentIdentifier;
        if (array_search($docId, $includeIds)) {
            $strJavascript = $modx->getChunk("LightBox");
        }
        return($strJavascript);
        

        In my template :
        [[includeJsSnippet? &includeIds=`18,40,41`]]
        

        In my chunk
        <script type="text/javascript" src="[(base_url)]js/prototype.js"></script>
        <script type="text/javascript" src="[(base_url)]js/scriptaculous.js?load=effects"></script>
        <script type="text/javascript" src="[(base_url)]js/lightbox.js"></script>
        <link rel="stylesheet" href="[(base_url)]css/lightbox.css" type="text/css" media="screen" />";
        

        It works.

        Am I doing the right thing including [(base_url)] in all src?

        Michel.
          • 7923
          • 4,213 Posts
          yes, base_url is needed if you enable friendly alias paths


            "He can have a lollipop any time he wants to. That's what it means to be a programmer."
            • 22303 MODX Staff
            • 10,725 Posts
            Quote from: doze at Nov 07, 2006, 12:16 PM

            yes, base_url is needed if you enable friendly alias paths

            Well, the easier way is to simply define this in your HTML HEAD element, before any elements that contain a src or href attribute:
            <base href="[(site_url)]" />

            Using this approach you do not need to use [(base_url)] in front of every path.
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              So what’s the difference between base_url and site_url?
                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
                • 7923
                • 4,213 Posts
                if I have modx installed in a subfolder in my siteroot called "modx", base_url is "/modx/" site_url is "www.mydomain.com/modx/"


                  "He can have a lollipop any time he wants to. That's what it means to be a programmer."
                  • 22303 MODX Staff
                  • 10,725 Posts
                  Right, except site_url is the full URL, including scheme (http://domain.tld/modx/), while base_url is the absolute URL from the root of the webserver (/modx/).
                    • 33706
                    • 51 Posts
                    you mention

                    Well, the easier way is to simply define this in your HTML HEAD element, before any elements that contain a src or href attribute:

                    I was including it...but after my all my elements declarations. !!!!!!

                    Thanks a lot.

                    Michel.