Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding JavaScripts Dynamically

0 views
Skip to first unread message

Laser Lips

unread,
Jul 2, 2009, 7:09:46 AM7/2/09
to
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

Jorge

unread,
Jul 2, 2009, 7:26:59 AM7/2/09
to

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...

--
Jorge.

Jorge

unread,
Jul 2, 2009, 7:32:54 AM7/2/09
to
Jorge wrote:
> (...)

> Are you doing it in IE ?

read: is the DOM ready ? (only applies to our much-beloved IE)

--
Jorge.

David Mark

unread,
Jul 2, 2009, 6:27:41 PM7/2/09
to
On Jul 2, 7:09 am, Laser Lips <loudsphi...@gmail.com> wrote:
> 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]

Jorge

unread,
Jul 4, 2009, 12:20:00 PM7/4/09
to
On Jul 3, 12:27 am, David Mark <dmark.cins...@gmail.com> wrote:
> On Jul 2, 7:09 am, Laser Lips <loudsphi...@gmail.com> wrote:
>
> > 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.

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/

--
Jorge.

0 new messages