views:

20

answers:

2

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.

A: 

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>
jeroen
How do includes work in basic HTML? These are not even PHP pages I'm working on
Urda
+1  A: 

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.

John Feminella
Don't use XML self-closing tag syntax. The question is tagged html, and it isn't suitable for HTML-Compatible XHTML either.
David Dorward
Corrected. Thanks for the heads-up.
John Feminella