![]() |
How do you write JavaScript in document.write and have it workproperly?
I'll probably be using jquery instead of document.write to write
<script src="http://example.com/script.js">myfunction();</script> into a div tag, but I can't get it work right in all the browsers. IE doesnt seem to like it when JS writes JS. Unfortunately I can't modify something on the serverside to write it from the backend, so I can only do it with JS. Any ideas? |
Re: How do you write JavaScript in document.write and have it work properly?
Frank Peterson wrote on 11 jun 2009 in comp.lang.javascript:
> I'll probably be using jquery instead of document.write to write > > <script src="http://example.com/script.js">myfunction();</script> > If a script declaration has a declared source, your content "myfunction();" is not executed. Do not use jquery till you know enough about javascript to know you better do without it. -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress) |
Re: How do you write JavaScript in document.write and have it workproperly?
Frank Peterson wrote:
> I'll probably be using jquery instead of document.write to write > > <script src="http://example.com/script.js">myfunction();</script> > > into a div tag, but I can't get it work right in all the browsers. IE > doesnt seem to like it when JS writes JS. <script type="text/javascript"> document.write( '<script type="text/javascript" src="foo.js"><\/script>'); </script> -- Martin Honnen http://msmvps.com/blogs/martin_honnen/ |
Re: How do you write JavaScript in document.write and have it workproperly?
Frank Peterson wrote:
> I'll probably be using jquery instead of document.write to write > > <script src="http://example.com/script.js">myfunction();</script> If you are lucky, myfunction(); is considered the alternative if the `src' script cannot be loaded; if not, it will be ignored. The least you need is <script type="text/javascript" src="http://example.com/script.js"></script> <script type="text/javascript">myfunction();</script> provided that `myfunction' is declared in `script.js'. > into a div tag, but I can't get it work right in all the browsers. Of course not. jQuery is bloated junk. > IE doesnt seem to like it when JS writes JS. Or you might not know what you are doing. > Unfortunately I can't modify something on the serverside to write > it from the backend, so I can only do it with JS. It escapes me why you want to do that. Either you can modify the source code, then you can simply write the `script' element as it is. Or you can't, then you can call myfunction() without including a `script' element somewhere. In any case, what you are trying to do is unreliable at best. PointedEars |
Re: How do you write JavaScript in document.write and have it work properly?
Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote in
news:4A313001.20800@PointedEars.de: > In any case, what you are trying to do is unreliable at best. TL is usually correct about these sorts of things. I'm not sure if he means unreliable in the sense that it cannot be guaranteed to work on all browsers, or under certain unspecified conditions depending on what else you are trying to do. Nonetheless, I have been pretty successful at writing javascript to a document in order to "fake" dynamic code on a static server. Take a look at: http://pages.videotron.com/duffym/poetry.htm and take the poetry exam at the bottom of the page. After you have passed the exam, it will re-direct you to my "Javascript Phone Poem Generator", which will take any natural number and then uses the Poem Factor Theorem to generate a completely new web page which is a rhyming algorithm that evaluates to the given number. It uses "document.write" to overwrite the current document, incuding some javascript to take care of the counter. It works on all browsers I have tested it with (IE 6&7, FF3, Opera, Dillo). |
Re: How do you write JavaScript in document.write and have it workproperly?
On 6/11/2009 9:18 AM, Martin Honnen wrote:
> Frank Peterson wrote: >> I'll probably be using jquery instead of document.write to write >> >> <script src="http://example.com/script.js">myfunction();</script> >> >> into a div tag, but I can't get it work right in all the browsers. IE >> doesnt seem to like it when JS writes JS. > > <script type="text/javascript"> > document.write( > '<script type="text/javascript" src="foo.js"><\/script>'); > </script> Just the same... Ewww... -- Michael J. Ryan - http://tracker1.info/ .... The key to immortality is first to live a life worth remembering. |
Re: How do you write JavaScript in document.write and have it workproperly?
>
> * <script type="text/javascript"> > * document.write( > '<script type="text/javascript" src="foo.js"><\/script>'); > * </script> > > -- > I've done that before and IE didnt like it. Luckily I found a way in our backend templates to accomplish what we needed, so I dont have to full around with JS. |
Re: How do you write JavaScript in document.write and have it workproperly?
frank peterson wrote:
>> <script type="text/javascript"> >> document.write( >> '<script type="text/javascript" src="foo.js"><\/script>'); >> </script> >> >> -- >> > > I've done that before and IE didnt like it. Yours wasn't like that then, because that works and IE likes it. |
Re: How do you write JavaScript in document.write and have it workproperly?
On Jun 13, 2:04*am, "Michael J. Ryan" <m...@tracker1.info.invalid>
wrote: > On 6/11/2009 9:10 AM, Frank Peterson wrote: > > > I'll probably be using jquery instead of document.write to write > > > <script src="http://example.com/script.js">myfunction();</script> > > > into a div tag, but I can't get it work right in all the browsers. IE > > doesnt seem to like it when JS writes JS. Unfortunately I can't modify > > something on the serverside to write it from the backend, so I can > > only do it with JS. > > > Any ideas? > > What is it you are *really* trying to accomplish here? *dynamically loading > javascript? *Or dynamically replacing content? *It's considered bad form to > use document.write in javascript over using even .innerHTML off of a selected > dom element. That's nonsense. The former is occasionally useful and the latter is definitely bad form (and should never be used before the document is ready, so it isn't a substitute for the former.) [snip] |
Re: How do you write JavaScript in document.write and have it workproperly?
On Jun 13, 2:08*am, "Michael J. Ryan" <m...@tracker1.info.invalid>
wrote: > On 6/11/2009 9:18 AM, Martin Honnen wrote: > > > Frank Peterson wrote: > >> I'll probably be using jquery instead of document.write to write > > >> <script src="http://example.com/script.js">myfunction();</script> > > >> into a div tag, but I can't get it work right in all the browsers. IE > >> doesnt seem to like it when JS writes JS. > > > *<script type="text/javascript"> > > *document.write( > > '<script type="text/javascript" src="foo.js"><\/script>'); > > *</script> > > Just the same... Ewww... > LOL. I'd like to see your replacement that uses innerHTML. |
| All times are GMT. The time now is 08:12 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.