简体   繁体   中英

How do I load Javascript from an external file?

What is the best way to load a common Javascript chunk of code from a file on a simple website? I need to add a script that tracks users for the Piwiki Open Source Analytics. I have hardly worked with Javascript, and just wanted to know what should I do?

<!-- Piwik -->
blah blah blah
<!-- End Piwik Tag -->

Basically, I don't want to have to update the entire site if I make a change.

Include this wherever you would like that script to appear:

<script type="text/javascript" src="scripts/code.js"> </script>

Then, in scripts/code.js , include the relevant analytics code.

Just put the javascript in a file and include that file in every page.

Normally, you put it at the end of the html, just before the end of the body, so it would look something like:

...
<script type="text/javascript" language="JavaScript" src="/my_scripts/piwiki.js"></script>
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM