By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,261 Members | 1,688 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,261 IT Pros & Developers. It's quick & easy.

Conditional loading of a remote script using document.write ... or?

P: n/a
I need to be able to conditionally load a remote script, using
the "<script src=..." syntax. Or some other way. I've seen
people writing about using the document.write method, but it's
not working like I want it to.

What's happening is, if I do a document.write(scriptcall), the
script loads, although sometimes very slowly... (?). Any inline
code is executed (I used an alert to verify that the script is
loading). But all the previous content of the page goes 'poof'.

If I look at the source of the page in this state, all I see
is the script tag I've document.written. I've tried using
document.close after writing the tag, and not using it, but
the results are the same. A blank page.

Wish there was a "loadscript(src)" function I could use.

Anybody know how to do what I want to do?
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ivo
"Razzbar" wrote
I need to be able to conditionally load a remote script, using
the "<script src=..." syntax. Or some other way.
That syntax would not be reliable in a contional way. Try this:

if ( foo ) {
var bar = document.createElement('script');
bar.src = 'mypath/myscript.js';
}
I've seen people writing about using the document.write
method, but it's not working like I want it to.
If I look at the source of the page in this state, all I see
is the script tag I've document.written.


document.write() should only be used during the initial load of the page, if
at all. Upon loading, the document is closed and writing to it would result
in a new document being generated. This is what you see in the source.
HTH
Ivo
Jul 23 '05 #2

P: n/a
"Ivo" <no@thank.you> wrote in message news:<40***********************@news.wanadoo.nl>.. .
"Razzbar" wrote
I need to be able to conditionally load a remote script, using
the "<script src=..." syntax. Or some other way.
That syntax would not be reliable in a contional way. Try this:

if ( foo ) {
var bar = document.createElement('script');
bar.src = 'mypath/myscript.js';

document.body.appendChild(bar); }
Thank you! It didn't work, but I added the appendChild line,
and it's all hunky-dory.
I've seen people writing about using the document.write
document.write() should only be used during the initial load of the page, if
Besides, I think it's just a hokey kludgey way to do things. IMHO.
at all. Upon loading, the document is closed and writing to it would result
in a new document being generated. This is what you see in the source.
Exactly. It takes forever, then you see a blank page. Worst, you can't
reload the original page. Even with a pragma no-cache, if you reload
you get the .js file. You just can't get the original page back -- in
IE6. Probably browser specific. Too much of that going around.


HTH


You did. Thanks.
Jul 23 '05 #3

P: n/a
However, regarding loading of script by setting .src,
see the comments regarding Mozilla / Netscape at
http://groups.google.com/groups?thre...eda.datanet.hu

Csaba Gabor

"Razzbar" <gl***@potatoradio.f2s.com> wrote in message
news:c4**************************@posting.google.c om...
"Ivo" <no@thank.you> wrote in message news:<40***********************@news.wanadoo.nl>.. .
"Razzbar" wrote
I need to be able to conditionally load a remote script, using
the "<script src=..." syntax. Or some other way.


That syntax would not be reliable in a contional way. Try this:

if ( foo ) {
var bar = document.createElement('script');
bar.src = 'mypath/myscript.js';

document.body.appendChild(bar);
}


Thank you! It didn't work, but I added the appendChild line,
and it's all hunky-dory.
I've seen people writing about using the document.write
document.write() should only be used during the initial load of the

page, if
Besides, I think it's just a hokey kludgey way to do things. IMHO.
at all. Upon loading, the document is closed and writing to it would

result in a new document being generated. This is what you see in the source.


Exactly. It takes forever, then you see a blank page. Worst, you can't
reload the original page. Even with a pragma no-cache, if you reload
you get the .js file. You just can't get the original page back -- in
IE6. Probably browser specific. Too much of that going around.


HTH


You did. Thanks.

Jul 23 '05 #4

P: n/a
Ivo
"Csaba Gabor" pointed out:
However, regarding loading of script by setting .src,
see the comments regarding Mozilla / Netscape at
http://groups.google.com/groups?thre...eda.datanet.hu


The discussion in that thread is about a changing the source of an already
existing script while this one was about setting the source of a newly
created script. Even those browsers see a difference in that.
Ivo
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.