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

Discussion in 'Javascript' started by Frank Peterson, Jun 11, 2009.

  1. 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?
     
    Frank Peterson, Jun 11, 2009
    #1
    1. Advertising

  2. Frank Peterson

    Evertjan. Guest

    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)
     
    Evertjan., Jun 11, 2009
    #2
    1. Advertising

  3. 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/
     
    Martin Honnen, Jun 11, 2009
    #3
  4. 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
     
    Thomas 'PointedEars' Lahn, Jun 11, 2009
    #4
  5. Frank Peterson

    Mike Duffy Guest

    Re: How do you write JavaScript in document.write and have it work properly?

    Thomas 'PointedEars' Lahn <> wrote in
    news::

    > 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).
     
    Mike Duffy, Jun 11, 2009
    #5
  6. 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.
     
    Michael J. Ryan, Jun 13, 2009
    #6
  7. >
    >   <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.
     
    frank peterson, Jun 22, 2009
    #7
  8. Frank Peterson

    Stevo Guest

    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.
     
    Stevo, Jun 22, 2009
    #8
  9. Frank Peterson

    David Mark Guest

    On Jun 13, 2:04 am, "Michael J. Ryan" <>
    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]
     
    David Mark, Jun 28, 2009
    #9
  10. Frank Peterson

    David Mark Guest

    On Jun 13, 2:08 am, "Michael J. Ryan" <>
    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.
     
    David Mark, Jun 28, 2009
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ennio
    Replies:
    2
    Views:
    870
    Peter Van Weert
    Sep 7, 2006
  2. Mufasa
    Replies:
    7
    Views:
    366
    Mufasa
    Jun 11, 2007
  3. shoplifes
    Replies:
    0
    Views:
    311
    shoplifes
    Nov 25, 2007
  4. Aston J.
    Replies:
    13
    Views:
    624
    Justin Collins
    Mar 16, 2011
  5. Jake Barnes
    Replies:
    15
    Views:
    184
    Thomas 'PointedEars' Lahn
    Feb 7, 2006
Loading...

Share This Page