pr**********@gmail.com wrote:
How to call a js file from another js file?
It is not sure what you want. Within an HTML document if you have
<script type="text/javascript" src="file1.js"></script>
and then later
<script type="text/javascript" src="file2.js"></script>
then the global variables and functions declared in file1.js are
available to the script code in file2.js.
If you want to insert another <script> element while the page load then try
document.write(
'<script type="text/javascript" src="file2.js"><\/script>');
If you want to dynamically load a script file after the document has
been loaded then you could use
var scriptElement;
if (document.createElement && (scriptElement =
document.createElement('script')))
{
script.type = 'text/javascript';
script.src = 'file3.js';
document.getElementsByTagName('head')[0].appendChild(script);
}
but you need to be aware that some browsers while supporting the DOM
createElement/appendChild had initially troubles ensuring that the
referenced script file was loaded dynamically so the above might not
load the script in very old Mozilla versions, early Opera 7 versions I
think and other browsers.
And even in browsers versions where the script file is loaded you need
to be aware that the loading happens asynchronously so calling functions
in file3.js safely is only possible after the file has been loaded and
the browser has signalled that. It is only possible with browser
dependent load or readystatechange event handlers.
--
Martin Honnen
http://JavaScript.FAQTs.com/