faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

8 of 9 people (89%) answered Yes
Recently 8 of 9 people (89%) answered Yes

Entry

How can I get a JavaScript code in a separate file to be called and executed by several HTML pages?

Apr 6th, 2008 18:40
ha mo, Colin Fraser, Joćo Martins,


You might think that such a fundamental question as this would deserve 
to be scorned by any JavaScript user, but it should not. The technique 
is so simple that it is mostly overlooked as being "trivial" in many 
text books on this subject. New users often have a great deal of 
difficulty in finding out how to do this because authors treat this 
question with such disdain by giving it one or two lines in a 600 page 
book.  
Simply put, in the <head>:
<script src="myscriptfile.js" type="text/JavaScript"> <!--//hide
//there is usually nothing here
//most scripts are placed in the remote *.js file
//unhide -->
</script>
Note the name of the file is "whatever.js" The remote file only needs be 
setup with things like function names and code. Remote *.js files are 
handled in much the same way as CSS files, once the link is established 
you dont need anything else except the raw code.     
Good luck...
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com