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

    I am new to using modx and I would like to know how you would link an external javascript file to a document. Your help would be really appreciated.

    Thanks
    Vi
      Vinzy
      • 15469
      • 64 Posts
      I´d put a Template Variable in the head-section of my template to keep parts of it dynamic.
      Then, you can put some CSS, JS or whatever you like in that TV, depending on your needs on a specific document.
      <html>
      <head>
      <title>[*pagetitle*]</title>
      <meta>...</meta>
      [*sampleTV*]
      </head>
      <body>
      [*content*]
      </body>
      </html>
      


      Content of [*sampleTV*] might be then:
      <script type="text/javascript" src="[(baseurl)]assets/templates/your_template_here/your_jsdir_here/sample.js"></script>
      <script type="text/javascript">
      alert("Sample!");
      </script>
      

        $cd /pub
        $more beer
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Or you can use a snippet in your document, have the snippet use one of these, depending on where you want the javascript tags placed (in the head or in the body of the html page)

        http://wiki.modxcms.com/index.php/API:regClientStartupScript
        http://wiki.modxcms.com/index.php/API:regClientScript
          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