Reputation: 407
In C I'm accustomed to do something like:
//MyHeaderFile.h
#define MY_CONSTANT 34
//MyMainFile.c
#include MyHeaderFile.h
int num = MY_CONSTANT;
I want do something in an html document like:
//MyJS.js
#define MY_SCRIPT <script>some javascript stuff </script>
//MyHTML.html
<html>
MY_SCRIPT
</html>
This html would execute whatever script code was defined as MY_SCRIPT. Basically What I want is to have multiple .html files reference the javascript code, all of them executing the same code defined in the .js file. It would just be nice to be able to change the code in the .js file once and have it affect all of the html files at once.
Any ideas?
Upvotes: 0
Views: 645
Reputation: 1598
You can have many files as you want. For example in a file called "header.js" you can put this:
var MY_CONSTANT = 34;
And in your html file simply put the reference to that file:
<script type="text/javascript" src="header.js>
<script type="text/javascript">
//You can use MY_CONSTANT here
var myNumber = MY_CONSTANT;
</script>
You can have many files as you want, but be sure to put the header.js before another scripts
Upvotes: 0
Reputation: 71
Reference your JS file in a script element as such
<script src="(LOCATION OF JS FILE)"></script>
This will cause the Javascript code to execute when the element loads.
Check out this tutorial for more info
http://www.w3schools.com/tags/tag_script.asp
Upvotes: 1