473,394 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to call a js file from another js file?

How to call a js file from another js file?

Oct 3 '05 #1
7 3227


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
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
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
Thank u
The code is working!!!!!!!!!!!!!!!!

Oct 7 '05 #5
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Greg Chapman | last post by:
I am at my wit's end trying to get information out of Streamline.net's support dept about my problem. They reply quickly enough, but seem to try and give out the least possible amount of info each...
1
by: bjam | last post by:
Hi, if I want to include a set of call template methods into my xsl file can I do that? Meaning I want to have the template method below of data_output in another file that I can use in several...
3
by: JoeK | last post by:
Hey all, I am automating a web page from Visual Foxpro. I can control all the textboxes, radio buttons, and command buttons using syntax such as: ...
10
by: bienwell | last post by:
Hi, I have a question about file included in ASP.NET. I have a file that includes all the Sub functions (e.g FileFunct.vb). One of the functions in this file is : Sub TestFunct(ByVal...
5
by: SStory | last post by:
Hi all, I really needed to get the icons associated with each file that I want to show in a listview. I used the follow modified code sniplets found on the internet. I have left in...
0
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode...
6
by: mygoogleaccount | last post by:
Hi, may be someone could help me ? i need to use cyrillic letters in a php application. I changed everything to UTF-8 and it works fine. The only problem are CYRILLIC SMALL LETTER ES...
7
by: Steve_Black | last post by:
Hello, I'm toying with the idea of loading a MenuStrip (VB.Net 2005) dynamically based on who is logged into my system. Every user has different security settings and I want to customize the...
1
by: lele1979 | last post by:
Hi XSL Community, I would do a file in PDF with FO and XSL/XML tecnology. In a precisely moment of a creation of this PDF file I must call a precisely template from a precisely XSL file, that...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.