CES said the following on 11/20/2005 7:23 PM:[color=blue]
> Randy Webb wrote:
>[color=green]
>> CES said the following on 11/20/2005 6:12 PM:
>>[color=darkred]
>>> All,
>>> I was wondering if their is a way of loading an external script fill
>>> from within a script??[/color]
>>
>>
>> Yes, and the benefit is that it is more widely supported than an
>> HTTPRequest Object is.[/color][/color]
Answer to Question One ^^^^^^^^^
[color=blue][color=green]
>>[color=darkred]
>>> function test(var){
>>> <script language="javascript" src="../scripts/base.js"
>>> type="text/javascript" />
>>> }[/color]
>>
>>
>> Why is var passed into the function if you don't use it? But, var is
>> probably the worst variable name to use.
>>[color=darkred]
>>> </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...[/color]
>>
>>
>> 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[color=darkred]
>> >[/color]
>>
>> 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.
>>[/color]
>
> Sorry... I forgot to ad the appendChild to the script above:
>
> --------external file base.js -------
> var loadGoogleScript = document.createElement('script');
> loadGoogleScript.type = "text/javascript";
> loadGoogleScript.src = "http://www.google-analytics.com/urchin.js";
> document.getElementsByTagName('head')[0].appendChild(loadGoogleScript);
>
> 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[/color]
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.js" 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(fileURL){
var loadGoogleScript = document.createElement('script');
loadGoogleScript.type = "text/javascript";
loadGoogleScript.src = fileURL;
document.getElementsByTagName('head')[0].appendChild(loadGoogleScript);
}
and then call it like this:
loadJSFile('http://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.javascript FAQ -
http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices -
http://www.JavascriptToolbox.com/bestpractices/