Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Seperate file for JavaScript code.

Status
Not open for further replies.

Neily

Programmer
Jul 27, 2000
342
GB
Can someone help me to set-up this.

I want a seperate file that contains all my code for a web site and the html files just refer to this when they run. I have never done this so would like your help. An example would be great!!

Thanks Neil
 
Save them to a seperate file without any html except the script tags.

ex.

<script language=&quot;JavaScript&quot;>
<!--
function myfunction() {
x=1
}
//-->
</script>

Then &quot;include&quot; the file in your html page:

<!--#include file=&quot;myfunctions.inc&quot;-->

 
What you want to do is save your script in separate .js files, such as
Code:
myfunction.js

In your web pages you can then use a pair of script tags to reference the file, as in:
Code:
<script language=vbscript src=&quot;myfunction.js&quot;></script>
Some would argue that the &quot;include&quot; approach is improper. However it will only work if you have some sort of server-side include facility at your disposal.

Using the &quot;proper&quot;
Code:
src[\code] attribute of the [code]<script>
tag should work for any browser, and almost any server including free hosting servers.
 
By the way, your .js file(s) would not contain <script> tags, only code.
 
Thanks for that. But how do I call the functions from the .js file. I Keep getting errors. Do you have to call them from a certain place.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top