HighDots Forums  

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

Javascript JavaScript language (comp.lang.javascript)


Discuss Conditional loading of a remote script using document.write ... or? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Razzbar
 
Posts: n/a

Default Conditional loading of a remote script using document.write ... or? - 05-01-2004 , 08:59 PM






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?

Reply With Quote
  #2  
Old   
Ivo
 
Posts: n/a

Default Re: Conditional loading of a remote script using document.write ... or? - 05-01-2004 , 09:18 PM






"Razzbar" wrote
Quote:
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';
}

Quote:
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




Reply With Quote
  #3  
Old   
Razzbar
 
Posts: n/a

Default Re: Conditional loading of a remote script using document.write ... or? - 05-04-2004 , 04:54 PM



"Ivo" <no (AT) thank (DOT) you> wrote

Quote:
"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.



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

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





Quote:
HTH
You did. Thanks.


Reply With Quote
  #4  
Old   
Csaba Gabor
 
Posts: n/a

Default Re: Conditional loading of a remote script using document.write ... or? - 05-04-2004 , 05:05 PM



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" <glakk (AT) potatoradio (DOT) f2s.com> wrote

Quote:
"Ivo" <no (AT) thank (DOT) you> wrote

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



Reply With Quote
  #5  
Old   
Ivo
 
Posts: n/a

Default Re: Conditional loading of a remote script using document.write ... or? - 05-04-2004 , 05:58 PM



"Csaba Gabor" pointed out:
Quote:
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




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.