473,545 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3246


pr**********@gm ail.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.creat eElement && (scriptElement =
document.create Element('script ')))
{
script.type = 'text/javascript';
script.src = 'file3.js';
document.getEle mentsByTagName( 'head')[0].appendChild(sc ript);
}
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 readystatechang e 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.creat eElement && (scriptElement =
document.create Element('script ')))
{
scriptElement.t ype = 'text/javascript';
scriptElement.s rc = 'one.js';

document.getEle mentsByTagName( 'head')[0].appendChild(sc riptElement);
}

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**********@gm ail.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.creat eElement && (scriptElement =
document.create Element('script ')))
{
script.type = 'text/javascript';
script.src = 'file3.js';
document.getEle mentsByTagName( 'head')[0].appendChild(sc ript);
}
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 readystatechang e event handlers.

--

Martin Honnen
http://JavaScript.FAQTs.com/


Oct 6 '05 #3
pr**********@gm ail.com said the following on 10/6/2005 9:01 AM:
Thanks for the reply
I tried with the code

var scriptElement;
if (document.creat eElement && (scriptElement =
document.create Element('script ')))
{
scriptElement.t ype = 'text/javascript';
scriptElement.s rc = 'one.js';

document.getEle mentsByTagName( 'head')[0].appendChild(sc riptElement);
}

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.javas cript 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**********@gm ail.com wrote:
Thank u
The code is working!!!!!!!! !!!!!!!!


Oct 18 '05 #6

pr**********@gm ail.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**********@gm ail.com wrote:
Thank u
The code is working!!!!!!!! !!!!!!!!

Hello Priya,
scriptElement.s rc = '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.s rc = '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
5268
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 time. The transcript so far is reproduced for your amusement below. To summarise: I've put up a Sudoku-solving program called Sudoku.exe. I...
1
1574
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 other xsl files when needed. This would be similar to an inclue mindset in C++ or a import in java. Any help is greatly appreciated. Below I would...
3
4020
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: oIE.Document.Forms("searchform").Item(<name>).Value = <myvalue> But I cannot control a dropdown with an onchange event. I can set the dropdown's value and selectedIndex, but then calling the...
10
24048
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 strInput As String) return (strInput & " test") End Sub
5
2294
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 commented code for anyone else who may be looking to do the same.
0
2937
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 thread. so far so good. all contacts r added properly. now when another login adds me in his contact, i recv a subscription, so i popup a form and ask...
6
4257
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 CYRILLIC SMALL LETTER YA
7
10049
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 main menu instead of showing all possible options and only enabling/disabling certain ones. I have a table that stores the menu item name, parent...
1
1678
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 I have already included. The name of this template is the same that from a file XML.
0
7490
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7682
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7935
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7780
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6009
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3465
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.