How to call a js file from another js file?

M

Martin Honnen

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.
 
P

priya.tweety

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 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.
 
R

Randy Webb

(e-mail address removed) 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?
 
P

priya.tweety

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
 
W

web.dev

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

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';
 
P

priya.tweety

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.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top