insert a script element into the document body from an external script....

Discussion in 'Javascript' started by Brian, Jun 24, 2004.

  1. Brian

    Brian Guest

    Hi all.

    I have a bunch of pages that reference an external script from the
    head section. I'd like to add additional <script> elements to the
    pages' bodies, but I can't edit the pages themselves. I'd like to add
    them from the existing script, but I need to make sure that the
    elements get added to the end of the document's body section.

    Using document.write("<script ..."); the elements get added to the
    head section! I'm trying to use the following code, but it complains
    that the body element is null when it executes. Any idea how I can
    add external script references to a document from an existing external
    script?

    Thanks,
    Brian


    I've added this to the existing external script.
    ================================================
    var hbxScriptElement = document.createElement("script");
    hbxScriptElement.language = "JavaScript";
    hbxScriptElement.type = "text/javascript";
    hbxScriptElement.src = "shared/js/hbx.js";
    document.onload = addElementToBody(hbxScriptElement);

    function addElementToBody(el) {
    eval("document.getElementsByTagName('body')[0].appendChild(el)");
    }
    ================================================
     
    Brian, Jun 24, 2004
    #1
    1. Advertising

  2. Brian

    Randy Webb Guest

    Re: insert a script element into the document body from an externalscript....

    Brian wrote:

    > Hi all.
    >
    > I have a bunch of pages that reference an external script from the
    > head section. I'd like to add additional <script> elements to the
    > pages' bodies, but I can't edit the pages themselves. I'd like to add
    > them from the existing script, but I need to make sure that the
    > elements get added to the end of the document's body section.


    Why at the end of the body section? If you are using document.write to
    add them, then it won't matter where they get inserted, the current page
    would get over-written as its called after onload fires.

    > Using document.write("<script ..."); the elements get added to the
    > head section! I'm trying to use the following code, but it complains
    > that the body element is null when it executes. Any idea how I can
    > add external script references to a document from an existing external
    > script?


    Yes.



    > Thanks,
    > Brian
    >
    >
    > I've added this to the existing external script.
    > ================================================
    > var hbxScriptElement = document.createElement("script");
    > hbxScriptElement.language = "JavaScript";


    superfluous and unneeded.

    > hbxScriptElement.type = "text/javascript";
    > hbxScriptElement.src = "shared/js/hbx.js";
    > document.onload = addElementToBody(hbxScriptElement);
    >
    > function addElementToBody(el) {
    > eval("document.getElementsByTagName('body')[0].appendChild(el)");


    http://www.jibbering.com/faq/#FAQ4_40
    No eval needed.

    document.documentElement.appendChild(el);
    will add it to the documentElement, after the last closing tag is
    encountered.

    > }


    What exactly is it you are trying to accomplish though?


    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/
     
    Randy Webb, Jun 24, 2004
    #2
    1. Advertising

  3. Brian

    Lee Guest

    Brian said:
    >
    >Hi all.
    >
    >I have a bunch of pages that reference an external script from the
    >head section. I'd like to add additional <script> elements to the
    >pages' bodies, but I can't edit the pages themselves. I'd like to add
    >them from the existing script, but I need to make sure that the
    >elements get added to the end of the document's body section.


    You need to find another way to accomplish whatever your goal is.
    Why do you think this script block needs to be added at the end?

    The position of the block only matters before the page has been
    rendered, and, as you've found, you can't touch the body from the
    head until after it's been rendered.
     
    Lee, Jun 24, 2004
    #3
    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. Rick Spiewak
    Replies:
    5
    Views:
    871
    Lewis Wang [MSFT]
    Aug 1, 2003
  2. Mark Sandfox
    Replies:
    2
    Views:
    4,185
    Mark Sandfox
    Jun 24, 2004
  3. HANM
    Replies:
    2
    Views:
    763
    Joseph Kesselman
    Jan 29, 2008
  4. Replies:
    1
    Views:
    173
  5. Martin Rinehart

    document or document.body

    Martin Rinehart, Dec 6, 2008, in forum: Javascript
    Replies:
    13
    Views:
    190
    Martin Honnen
    Dec 7, 2008
Loading...

Share This Page