Friday, January 18, 2008

How to include one JavaScript file from another

Have one javascript file in header and one in footer for the whole
website, so that any changes in the script will reflects whole site.
Why you need two javascript ?
The header javascript must contains initalization or function code.The
footer javascript is for calling function code depends upon the existing
html elements. so that most error will reduce since the process is start
after all html element rendered.

Well, how can we include javascript from another?
Step 1: To include some JavaScript from an HTML file is simply:
<script language="JavaScript" type="text/javascript"
src="external.js"></script>

Step 2:Allow that external JavaScript file to cause another to be included, add
the following function to that first external JavaScript file:
// Function to allow one JavaScript file to be included by another.
// Copyright (C) 2006 www.cryer.co.uk
function IncludeJavaScript(jsFile)
{
document.write('<script type="text/javascript" src="'
+ jsFile + '"></script>');
}
IncludeJavaScript('js2.js');


Step 3:Then to include a second JavaScript file simply add the line:
IncludeJavaScript('secondJS.js');

Note:The function logic is to write Sript Tags in the document.
Resource:

http://www.cryer.co.uk/resources/javascript/script17_include_js_from_js.htm

No comments:

Adobe News