Urda
Urda

Reputation: 5411

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.

Upvotes: 1

Views: 149

Answers (2)

John Feminella
John Feminella

Reputation: 311745

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.

Upvotes: 1

jeroen
jeroen

Reputation: 91792

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>

Upvotes: 0

Related Questions