By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

How to call a js file from another js file?

P: n/a
How to call a js file from another js file?

Oct 3 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a


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/
Oct 3 '05 #2

P: n/a
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.

Thanks in advance,
Priya

Martin Honnen wrote:
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/


Oct 6 '05 #3

P: n/a
pr**********@gmail.com said the following on 10/6/2005 9:01 AM:
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.
Yes it does, but it does it dynamically.
I tried searching thru the html page after executing this code....but
couldnt find the object.I searched using the file name one.js.


Thats because you are not looking at the dynamic HTML of the page, you
are looking at the static source.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?
Oct 6 '05 #4

P: n/a
Thank u
The code is working!!!!!!!!!!!!!!!!

Oct 7 '05 #5

P: n/a
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

pr**********@gmail.com wrote:
Thank u
The code is working!!!!!!!!!!!!!!!!


Oct 18 '05 #6

P: n/a

pr**********@gmail.com wrote:
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

pr**********@gmail.com wrote:
Thank u
The code is working!!!!!!!!!!!!!!!!

Hello Priya,
scriptElement.src = 'one.js';


See that line above? Once you place your files on the server, you
should ensure that the path to your file is correct, that is,

scriptElement.src = 'path/one.js';

Oct 18 '05 #7

P: n/a
I gave the correct path. It attachs the script file to the head tag the
first time , but the function within the script file is not called for
the first time. Only when the button is clicked for the second time
the function gets executed.

I have placed the JS files in a folder in the server but i have given
the path correctly.

Oct 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.