Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How do you write JavaScript in document.write and have it workproperly?

Reply
Thread Tools

How do you write JavaScript in document.write and have it workproperly?

 
 
Frank Peterson
Guest
Posts: n/a
 
      06-11-2009
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?
 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      06-11-2009
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)
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      06-11-2009
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/
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      06-11-2009
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
 
Reply With Quote
 
Mike Duffy
Guest
Posts: n/a
 
      06-11-2009
Thomas 'PointedEars' Lahn <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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).
 
Reply With Quote
 
Michael J. Ryan
Guest
Posts: n/a
 
      06-13-2009
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.
 
Reply With Quote
 
frank peterson
Guest
Posts: n/a
 
      06-22-2009
>
> * <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.
 
Reply With Quote
 
Stevo
Guest
Posts: n/a
 
      06-22-2009
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.
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      06-28-2009
On Jun 13, 2:04*am, "Michael J. Ryan" <(E-Mail Removed)>
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]
 
Reply With Quote
 
David Mark
Guest
Posts: n/a
 
      06-28-2009
On Jun 13, 2:08*am, "Michael J. Ryan" <(E-Mail Removed)>
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
I have no idea, you didn't say what country state or city you're in and you can't even write a proper post anyways. richard Computer Support 7 08-01-2010 09:12 PM
RE;Kontki if you delete kontiki any program you loaded with it in it 'will not work I have tried it with three programs and none work anymore (if you se it just stop download) 1-Twitch Computer Support 5 04-23-2009 02:45 PM
You have come? Definitely you have not gone shoplifes Computer Support 0 11-25-2007 03:08 PM
if you have used createElement() and appendChild() on a page, how do you capture all the HTML to write to PHP? Jake Barnes Javascript 15 02-07-2006 06:31 AM



Advertisments