From: priya.tweety on
How to call a js file from another js file?

From: Martin Honnen on


priya.tweety(a)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/
From: priya.tweety on
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:
> priya.tweety(a)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/

From: Randy Webb on
priya.tweety(a)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?
From: priya.tweety on
Thank u
The code is working!!!!!!!!!!!!!!!!

 |  Next  |  Last
Pages: 1 2
Prev: isNum
Next: Getting IFRAME text