SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot Valera Selev's Avatar
    Join Date
    Apr 2004
    Location
    Lviv, Ukraine
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic creation of javascript with "document.write"

    Hi,

    With the following code I would like to achieve the effect of dynamic creation of code <script type="text/javascript">document.write("by script")</script>, appending it to <div id="placeholder"> and executing it there:

    HTML Code:
    <html>
      <head></head>
      <body>
      <div id="placeholder"></div>
      <br/>after placeholder<br />
     
      <script>
        var script=document.createElement('SCRIPT');
        script.type="text/javascript";
        script.text = 'document.write("by script")';
        document.getElementById('placeholder').appendChild(script);
      </script>
      </body>
    </html>
    However, as far as I can see in result, the "by script" text is shown after text "after placeholder" while that's not what's expected, so it should be before "after placeholder", inside <div id="placeholder">.

    Is it possible to solve this problem or that's just a technical limitation?

    PS. FYI - in the real world instead of document.write("by script") I've got javascript code which inserts banner from external bannermanagement system, so I would like to implement a feature of loading banner after the whole page is loaded.

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for this i'd probably use innerHTML to deliver the code. create a div or similar and insert the js code wrapped in the script tags.


  3. #3
    SitePoint Zealot Valera Selev's Avatar
    Join Date
    Apr 2004
    Location
    Lviv, Ukraine
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gRoberts View Post
    for this i'd probably use innerHTML to deliver the code. create a div or similar and insert the js code wrapped in the script tags.
    In case of using innerHTML, the external bannermanagement system code will be loaded twice - first time when it's put into a div and second time when we use innerHTML to copy content.

    So the actual problem is to not load the bannermanagement system code twice.

  4. #4
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll try and see what I can do when I get home. Unfortunately I don't have the time at the moment.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •