Delayed SCRIPT Loading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ebrandmark@aol.com

    #1

    Delayed SCRIPT Loading

    I am trying to load a SCRIPT SRC tag that will result in a
    document.write into a specific location in my web page but not call for
    the SCRIPT SRC until the end of the page (so as not to delay the page).

    The DEFER method won't work because the document.write just starts a
    new page.

    I was hoping to leave a DIV where the result should end up. Then at
    the end of the page create a child of this DIV or another DIV that lays
    on top of this DIV and place the results in there.

    I can't use the innerHTML property of the DIV as it seems that SCRIPT
    tags within that innerHTML are not executed and the result may have
    SCRIPT tags.

    I also can't use an IFRAME (which is the easy solution) for various
    reasons.

    I have tried to create a child SCRIPT element of the DIV - but the
    document.write still writes at the bottom of the screen.

    I also (a lot of requirements, I know) need this to be generic (I won't
    know ahead of time the x,y location) and want to use this on a lot of
    web pages.

    So I want something like this:

    Content....

    Placeholder for SCRIPT SRC

    More content

    Call for SCRIPT SRC but results go over (in) the Placeholder
    End of Page code.

    Thanks for any assistance.

    Ed Brandmark

  • RobG

    #2
    Re: Delayed SCRIPT Loading

    ebrandmark@aol. com wrote:[color=blue]
    > I am trying to load a SCRIPT SRC tag that will result in a
    > document.write into a specific location in my web page but not call for
    > the SCRIPT SRC until the end of the page (so as not to delay the page).
    >
    > The DEFER method won't work because the document.write just starts a
    > new page.[/color]

    Then don't use document.write. Use DOM methods to modify your
    page.
    [color=blue]
    >
    > I was hoping to leave a DIV where the result should end up. Then at
    > the end of the page create a child of this DIV or another DIV that lays
    > on top of this DIV and place the results in there.[/color]

    Reference your location using document.getEle mentById or similar.
    [color=blue]
    > I can't use the innerHTML property of the DIV as it seems that SCRIPT
    > tags within that innerHTML are not executed and the result may have
    > SCRIPT tags.[/color]

    If you insert a script into a page, it will run as if it was
    there when the page was loaded. You can also insert a script
    with functions, then call them later.

    Here's a sample HTML page:

    <html>
    <head>
    <title>play</title>
    <script type="text/javascript">
    function addScript(){
    var h = document.getEle mentsByTagName( 'head')[0];
    var s = document.create Element('script ');
    s.type = 'text/javascript';
    s.src = 'play.js'
    h.appendChild(s );
    }
    </script>
    </head>
    <body>
    <button onclick="addScr ipt();">Add script</button>
    <button onclick="hiMum( );">Hi mum</button>
    <br>
    <span id="outSpan"></span>
    </body>
    </html>

    Save the following as "play.js" in the same directory:

    if (document.getEl ementById && document.create TextNode){
    var tx0 = document.create TextNode('hi mum 0');
    document.getEle mentById('outSp an').appendChil d(tx0);
    }

    function hiMum() {
    if (document.getEl ementById && document.create TextNode){
    var o = document.getEle mentById('outSp an');
    o.appendChild(d ocument.createE lement('br'));
    o.appendChild(d ocument.createT extNode('hi mum 1'));
    }
    }


    Clicking in "Add script" adds the script to the page. When
    loaded, it runs and writes "Hi mum 0" to the page. Clicking on
    "Hi mum" will added "Hi mum 1" to the page. Clicking on "Hi mum"
    before then does nothing (or generates an error).


    --
    Rob

    Comment

    Working...