473,699 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Loading an external.JS script from within the script Tag (Can itbe dun)???

CES
All,
I was wondering if their is a way of loading an external script fill from within a script??

<script language="javas cript" type="text/javascript">
function test(var){
<script language="javas cript" src="../scripts/base.js" type="text/javascript" />
}
</script>

Obviously this would cause n error but this would give you an idea of what I'm looking to do. I know I can do this with a simple include but...

Sorry about the stupid question... Thanks in advance. - CES
Nov 23 '05 #1
17 2773
CES said the following on 11/20/2005 6:12 PM:
All,
I was wondering if their is a way of loading an external script fill
from within a script??
Yes, and the benefit is that it is more widely supported than an
HTTPRequest Object is.
<script language="javas cript" type="text/javascript">
Skip the language attribute. It's irrelevant, redundant and deprecated
in HTML.
function test(var){
<script language="javas cript" src="../scripts/base.js"
type="text/javascript" />
}
Why is var passed into the function if you don't use it? But, var is
probably the worst variable name to use.
</script>
Obviously this would cause n error but this would give you an idea of
what I'm looking to do. I know I can do this with a simple include but...
Looks like you want to dynamically load a .js file depending on a
variable without reloading the page.

<URL:
http://groups.google.com/group/comp....scoring%3Dd%26


Or, a tinyURL:

<URL: http://tinyurl.com/a8kte >

Is a thread where I explained, and got feedback, on how to do what you
are describing.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 23 '05 #2
CES
Randy Webb wrote:
CES said the following on 11/20/2005 6:12 PM:
All,
I was wondering if their is a way of loading an external script fill
from within a script??


Yes, and the benefit is that it is more widely supported than an
HTTPRequest Object is.
<script language="javas cript" type="text/javascript">


Skip the language attribute. It's irrelevant, redundant and deprecated
in HTML.
function test(var){
<script language="javas cript" src="../scripts/base.js"
type="text/javascript" />
}


Why is var passed into the function if you don't use it? But, var is
probably the worst variable name to use.
</script>
Obviously this would cause n error but this would give you an idea of
what I'm looking to do. I know I can do this with a simple include but...


Looks like you want to dynamically load a .js file depending on a
variable without reloading the page.

<URL:
http://groups.google.com/group/comp....scoring%3Dd%26
>


Or, a tinyURL:

<URL: http://tinyurl.com/a8kte >

Is a thread where I explained, and got feedback, on how to do what you
are describing.


Randy,

Sorry for the stupidity... unfortunately you responded before I had a chance to retract the post... This is what I'm trying to do from within an external .js file:

--------external file base.js (File Size is 17kb)-------
var loadGoogleScrip t = document.create Element('script ');
loadGoogleScrip t.type = "text/javascript";
loadGoogleScrip t.src = "http://www.google-analytics.com/urchin.js";

-------- HTML that calls the base.js -----------------
<head>
<script src="../scripts/base.js" type="text/javascript" />
</head>
This seems to load properly in IE but I've seen some posts that indicate this might not work in FireFox any Suggestions?
Also I know the external file loads asynchronously. .. is their a way of testing if the script has loaded before I call a function from within the script??

CES

Nov 23 '05 #3
CES
Randy Webb wrote:
CES said the following on 11/20/2005 6:12 PM:
All,
I was wondering if their is a way of loading an external script fill
from within a script??


Yes, and the benefit is that it is more widely supported than an
HTTPRequest Object is.
<script language="javas cript" type="text/javascript">


Skip the language attribute. It's irrelevant, redundant and deprecated
in HTML.
function test(var){
<script language="javas cript" src="../scripts/base.js"
type="text/javascript" />
}


Why is var passed into the function if you don't use it? But, var is
probably the worst variable name to use.
</script>
Obviously this would cause n error but this would give you an idea of
what I'm looking to do. I know I can do this with a simple include but...


Looks like you want to dynamically load a .js file depending on a
variable without reloading the page.

<URL:
http://groups.google.com/group/comp....scoring%3Dd%26
>


Or, a tinyURL:

<URL: http://tinyurl.com/a8kte >

Is a thread where I explained, and got feedback, on how to do what you
are describing.


Sorry... I forgot to ad the appendChild to the script above:

--------external file base.js -------
var loadGoogleScrip t = document.create Element('script ');
loadGoogleScrip t.type = "text/javascript";
loadGoogleScrip t.src = "http://www.google-analytics.com/urchin.js";
document.getEle mentsByTagName( 'head')[0].appendChild(lo adGoogleScript) ;

And just to elevate any confusion the urchin.js file size is 17kb, not the base.js

If anyone could anser the two questions above I wold be greatfull - CES
Nov 23 '05 #4
Randy Webb wrote:
[...]
Looks like you want to dynamically load a .js file depending on a
variable without reloading the page.

<URL:
http://groups.google.com/group/comp....scoring%3Dd%26
>


Or, a tinyURL:

<URL: http://tinyurl.com/a8kte >

Is a thread where I explained, and got feedback, on how to do what you
are describing.


Randy,

Further to CES's question, I asked a similar question previously
regarding dynamic loading of scripts. This seems to be a favourite of
yours, I was wondering if you can answer it:

<URL:http://groups.google.c o.uk/group/comp.lang.javas cript/browse_frm/thread/dbe0590360ce758 8/0d99f2f0e7dc8dc d?q=greasemonke y&rnum=1#0d99f2 f0e7dc8dcd>

In Firefox, dynamic scripts run but the content is not accessible to the
script that loaded it. setTimeout() can be used to break the nexus but
that seems like a real kludge.

Any hints? Is this a Firefox bug?

A simple test case is below. In Firefox, clicking 'load & call' firstly
prints the error message, then the one from test.js. Subsequent clicks
also print the message from the in-page script before the one from the
loaded script.

IE works as expected - the message from the loaded script appears first,
then the one from the function (with no errors).

The 'load & setTimeout' button works as I would expect the first to work
in Firefox (though setting the lag to 0ms usually causes a 'showText is
undefined' error).

The third button is just to test whether showText is currently defined
or not.
<script type="text/javascript">

function addCall(txt){
var oScript = document.create Element('script ');
oScript.type = 'text/javascript';
oScript.src = 'test.js';
document.getEle mentsByTagName( 'head')[0].appendChild(oS cript);
try {
showText(txt);
} catch(e) {
document.getEle mentById('msg') .innerHTML +=
'showText failed...<br>';
}
}

function addTimeout(txt) {
var oScript = document.create Element('script ');
oScript.type = 'text/javascript';
oScript.src = 'test.js';
document.getEle mentsByTagName( 'head')[0].appendChild(oS cript);
setTimeout('sho wText(\''+txt+' \')', 50);
}

</script>

<input type="button" value="load & call"
onclick="addCal l('from ' + this.value);">
<br>
<input type="button" value="load & setTimeout"
onclick="addTim eout('from ' + this.value);">
<br>
<input type="button" value="typeof showText"
onclick="alert( 'typeof showText: ' + typeof showText);">

<div id="msg"></div>
/***********
test.js is:
***********/

function showText(txt)
{
document.getEle mentById('msg') .innerHTML += txt + '<br>';
}

showText('hi from test.js');


--
Rob
Nov 23 '05 #5
RobG said the following on 11/20/2005 8:28 PM:
Randy Webb wrote:
[...]
Looks like you want to dynamically load a .js file depending on a
variable without reloading the page.

<URL:
http://groups.google.com/group/comp....scoring%3Dd%26
>
Or, a tinyURL:

<URL: http://tinyurl.com/a8kte >

Is a thread where I explained, and got feedback, on how to do what you
are describing.


Randy,

Further to CES's question, I asked a similar question previously
regarding dynamic loading of scripts. This seems to be a favourite of
yours, I was wondering if you can answer it:

<URL:http://groups.google.c o.uk/group/comp.lang.javas cript/browse_frm/thread/dbe0590360ce758 8/0d99f2f0e7dc8dc d?q=greasemonke y&rnum=1#0d99f2 f0e7dc8dcd>
In Firefox, dynamic scripts run but the content is not accessible to the
script that loaded it. setTimeout() can be used to break the nexus but
that seems like a real kludge.

Any hints? Is this a Firefox bug?


I think it's more an implementation difference than a bug. It seems to
fall in the same category with the "attempting to update the display
while trying to execute a function" scenario where the browser won't
update the display until the function exits. In this case, Firefox isn't
updating(loadin g) the external file until the current function exits.
I think, but not sure, that the setTimeout (even at 0ms) is exiting the
function and putting the second function call in a second stack of
function calls rather than in the initial order.
A simple test case is below. In Firefox, clicking 'load & call' firstly
prints the error message, then the one from test.js. Subsequent clicks
also print the message from the in-page script before the one from the
loaded script.

IE works as expected - the message from the loaded script appears first,
then the one from the function (with no errors).
The 'load & setTimeout' button works as I would expect the first to work
in Firefox (though setting the lag to 0ms usually causes a 'showText is
undefined' error).


That would indicate what I thought to begin with. That the setTimeout is
allowing Firefox to update the script block before attempting to execute
the function.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 23 '05 #6
CES said the following on 11/20/2005 7:23 PM:
Randy Webb wrote:
CES said the following on 11/20/2005 6:12 PM:
All,
I was wondering if their is a way of loading an external script fill
from within a script??

Yes, and the benefit is that it is more widely supported than an
HTTPRequest Object is.
Answer to Question One ^^^^^^^^^
function test(var){
<script language="javas cript" src="../scripts/base.js"
type="text/javascript" />
}

Why is var passed into the function if you don't use it? But, var is
probably the worst variable name to use.
</script>
Obviously this would cause n error but this would give you an idea of
what I'm looking to do. I know I can do this with a simple include
but...

Looks like you want to dynamically load a .js file depending on a
variable without reloading the page.

<URL:
http://groups.google.com/group/comp....scoring%3Dd%26
>


Or, a tinyURL:

<URL: http://tinyurl.com/a8kte >

Is a thread where I explained, and got feedback, on how to do what you
are describing.


Sorry... I forgot to ad the appendChild to the script above:

--------external file base.js -------
var loadGoogleScrip t = document.create Element('script ');
loadGoogleScrip t.type = "text/javascript";
loadGoogleScrip t.src = "http://www.google-analytics.com/urchin.js";
document.getEle mentsByTagName( 'head')[0].appendChild(lo adGoogleScript) ;

And just to elevate any confusion the urchin.js file size is 17kb, not
the base.js

If anyone could anser the two questions above I wold be greatfull - CES


I answered the first one. Yes, you can dynamically load a js file from
script. In your case, you have the code and it does what you want it to
do. I am not sure why you are doing all that instead of a simple
<script src="someFile.j s" type="text/javascript"></script>
construct though. Unless it is for academic reasons. Or, you want to
modify it and make it function-alized:

function loadJSFile(file URL){
var loadGoogleScrip t = document.create Element('script ');
loadGoogleScrip t.type = "text/javascript";
loadGoogleScrip t.src = fileURL;
document.getEle mentsByTagName( 'head')[0].appendChild(lo adGoogleScript) ;
}

and then call it like this:

loadJSFile('htt p://www.google-analytics.com/urchin.js');

That is without the necessary feature detection since not all browsers
support gEBTN and appendChild.

As for knowing when the file is loaded, I doubt it very seriously unless
you loaded it from your own server, added some type of marker at the end
of the file, then retrieved it from there.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 23 '05 #7
On 2005-11-20, CES <no**@none.co m> wrote:

Sorry for the stupidity... unfortunately you responded before I had a chance to retract the post... This is what I'm trying to do from within an external .js file:

--------external file base.js (File Size is 17kb)-------
var loadGoogleScrip t = document.create Element('script ');
loadGoogleScrip t.type = "text/javascript";
loadGoogleScrip t.src = "http://www.google-analytics.com/urchin.js";

-------- HTML that calls the base.js -----------------
<head>
<script src="../scripts/base.js" type="text/javascript" />
</head>
This seems to load properly in IE but I've seen some posts that indicate
this might not work in FireFox any Suggestions?
Also I know the external file loads asynchronously. ..
is their a way of testing if the script has loaded before I call a function from within the script??


maybe you could use onload or if(function_nam e)

--

Bye.
Jasen
Nov 23 '05 #8
Here's what works in every browser. You just need to break up the
<script> tag within your script, and then drop in the variable:

<script language="javas cript" type="text/javascript">
function test(varScript) {
document.write( '<scr'+'ipt language="javas cript"
type="text/javascript" src="/mypath/js/' + varScript +
'"></scr'+'ipt>');
}

//function call
test(myFile.js)
</script>
Enjoy!

Nov 23 '05 #9
VK

Spats30 wrote:
You just need to break up the <script> tag within your script


I always did that since NN 3.0 by I never managed to find any official
explanation why the oposite way may lead to a failure. I know it may,
but I'm still cloudy why. Do you have some info about it?

Nov 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3238
by: mr_burns | last post by:
Hi, i was wondering if it is possible to load text into a string from an external text file. the reason is that i have a very large string and it is making my script very messy. also, is it possible to have some dynamic parts of the text from the text file? for example, if i load in a string and there is a part of it that inserts a value from a variable like the following: 'The number of people is ' + var_people_num;
5
2775
by: danny.myint | last post by:
I was under the assumption that javascript loads all the <head></head> elements before processing the <body> tag in Mozilla/Netscape/Firefox. It doesn't seem like it, with my problem. I have navigation that has onMouseOver and onMouseOut triggers, that use function calls in my external .js file. When I mouseOver a button in my navigation quickly enough to catch it before the rest of the body loads, it returns a function "not defined"...
5
5205
by: joaopedrogoncalves | last post by:
Hi, I want to load an external javascript file, get its results and stick them inside a <div> block. I also want to do this in several places on a web page. This way the browser doesn't have to wait for the external resource to load to show up the page, thus giving a perceiving faster load time for the user.
6
1695
by: Mellow Crow | last post by:
Just discovered this technique. Is this old hat? Would there be any disadvantage to doing this? In your external .js file: /* Summary: includes external scripts in this external script so that you don't have to reference them within the (x)html document files.
14
3184
by: Ilias Lazaridis | last post by:
within a python script, I like to create a collection which I fill with values from an external text-file (user editable). How is this accomplished the easiest way (if possible without the need of libraries which are not part of the standard distribution)? something like: text-file: {peter, 16},
1
3805
by: ozzy.osborn | last post by:
Hello All, I have been struggling with a cross browser solution to loading external javascript files on the fly. I have been successful using the following code in IE6: var newScr = document.createElement("SCRIPT"); newScr.src = "newScr.js"; newScr.type="text/javascript";
6
2768
by: Venkatesh | last post by:
Hello All, I have couple of doubts regarding the concept of on-demand javascript loading using javascript code. I could see on the net different techniques for achieving this - techniques like: 1. document.write("<script src= language='JavaScript'></script>); 2. sc = document.createElement("<script>"); sc.setAttribute("src", ); and append this to the head
10
2402
by: Stefan Weber | last post by:
Hi, I'm trying to access the JavaScript code contained in a <scripttag via its "text" attribute. This works well, if the code is embedded in the HTML page. However, when the code is in an external file with the "src" attribute, it does not work anymore. Does anybody have an idea if there is any way (be it clean and simple or as a workaround) to access the code of external scripts as well? I read, that if there is something like
20
4035
RMWChaos
by: RMWChaos | last post by:
Currently testing in: WinVista / IE7 I have been working on getting xmlhttprequest going for weeks now. I have finally gotten a semi-working script going. Pulling up text or xml files works great and populates into the webpage; however, executing javascript has a few problems: 1. Opens on a blank page, but not a new page. It appears to compeltely overwrite the original page. I want it to update the existing page by loading within a <div>...
0
8697
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8621
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8891
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7759
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5878
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2357
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2013
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.