P
priya.tweety
How to call a js file from another js file?
How to call a js file from another js file?
Martin said: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.
Thanks for the reply
I tried with the code
var scriptElement;
if (document.createElement && (scriptElement =
document.createElement('script')))
{
scriptElement.type = 'text/javascript';
scriptElement.src = 'one.js';
document.getElementsByTagName('head')[0].appendChild(scriptElement);
}
but it doesnt append this object in the document of the html page.
I tried searching thru the html page after executing this code....but
couldnt find the object.I searched using the file name one.js.
Hi
I still have problem calling the scripts dynamically
When the script files to be apended to the head tag are in the local
disk it works fine. I am also able to call the functions from the
script file.
But when the script files are put in a server not able to access it
only the second time.
The script tag is apended to the head tag of the html page but the
the function is not recognized
Help needed
regards,
Priya
scriptElement.src = 'one.js';
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.