Adding JavaScripts Dynamically

L

Laser Lips

Hi all.

I'm trying to add some scripts dynamically but it's not working.
I'm trying to do something like this...
var scripts=[];
scripts[0]="/csp/app/lib/t1.js";
scripts[0]="/csp/app/lib/t2.js";

for(var x=0;x<scripts.length;x++)
{
newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = scripts[x];
document.getElementsByTagName("head")[0].appendChild(newScript);
}

But neither script gets loaded.
I've tried using absolute and relative URL's
I've also tried loading scripts from other websites, but doesn’t work
for me.
Any ideas please?

Graham
 
J

Jorge

Laser said:
Hi all.

I'm trying to add some scripts dynamically but it's not working.
I'm trying to do something like this...
var scripts=[];
scripts[0]="/csp/app/lib/t1.js";
scripts[0]="/csp/app/lib/t2.js";

for(var x=0;x<scripts.length;x++)
{
newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.src = scripts[x];
document.getElementsByTagName("head")[0].appendChild(newScript);
}

But neither script gets loaded.
I've tried using absolute and relative URL's
I've also tried loading scripts from other websites, but doesn’t work
for me.
Any ideas please?

Use a debugger and see if they're effectively being inserted in the <head>.

Are you doing it in IE ?

Are you getting any error ?

Re-check their .src values.

You're assigning both urls to the same script[0]. I'd write var scripts=
["/csp/app/lib/t1.js", "/csp/app/lib/t2.js"]; instead.

You forgot to declare *var* newScript.

no more ideas...
 
D

David Mark

Hi all.

I'm trying to add some scripts dynamically but it's not working.

Did somebody tell you it would speed up loading? It won't.
I'm trying to do something like this...
var scripts=[];
scripts[0]="/csp/app/lib/t1.js";
scripts[0]="/csp/app/lib/t2.js";
Typo.


for(var x=0;x<scripts.length;x++)
{
   newScript = document.createElement('script');
   newScript.type = 'text/javascript';
   newScript.src = scripts[x];
   document.getElementsByTagName("head")[0].appendChild(newScript);

}

But neither script gets loaded.

How do you know?
I've tried using absolute and relative URL's

Stabbing around in the dark is no way to program (ask jQuery users.)

[snip]
 
J

Jorge

Did somebody tell you it would speed up loading?  It won't.

First, it speeds it up by preventing the latencies from adding up.
Second, it will or won't speed it up (even further) depending on
wether the servers are configured to throttle down the responses or
not (to a value below your internet connection bandwidth limit). For
example:

My internet connection's download bandwidth limit is: 650 KB/s.
The remote server is configured to serve files at no more than: 50KB/
s.

If I download a script at a time, I only get 50KB/s, but if I request
4 scripts at the same time, each one of them is sent to me @ 50KB/s,
therefore I see an incoming traffic of 200KB/s. That is a 4x speedup.

http://jorgechamorro.com/cljs/071/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,061
Latest member
KetonaraKeto

Latest Threads

Top