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

F

Frank Peterson

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?
 
E

Evertjan.

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

Martin Honnen

Frank said:
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>
 
T

Thomas 'PointedEars' Lahn

Frank said:
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
 
M

Mike Duffy

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

frank peterson

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

David Mark

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top