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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian

    #1

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

    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 hbxScriptElemen t = document.create Element("script ");
    hbxScriptElemen t.language = "JavaScript ";
    hbxScriptElemen t.type = "text/javascript";
    hbxScriptElemen t.src = "shared/js/hbx.js";
    document.onload = addElementToBod y(hbxScriptElem ent);

    function addElementToBod y(el) {
    eval("document. getElementsByTa gName('body')[0].appendChild(el )");
    }
    =============== =============== =============== ===
  • Randy Webb

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

    Brian wrote:
    [color=blue]
    > 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.[/color]

    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.
    [color=blue]
    > 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?[/color]

    Yes.


    [color=blue]
    > Thanks,
    > Brian
    >
    >
    > I've added this to the existing external script.
    > =============== =============== =============== ===
    > var hbxScriptElemen t = document.create Element("script ");
    > hbxScriptElemen t.language = "JavaScript ";[/color]

    superfluous and unneeded.
    [color=blue]
    > hbxScriptElemen t.type = "text/javascript";
    > hbxScriptElemen t.src = "shared/js/hbx.js";
    > document.onload = addElementToBod y(hbxScriptElem ent);
    >
    > function addElementToBod y(el) {
    > eval("document. getElementsByTa gName('body')[0].appendChild(el )");[/color]


    No eval needed.

    document.docume ntElement.appen dChild(el);
    will add it to the documentElement , after the last closing tag is
    encountered.
    [color=blue]
    > }[/color]

    What exactly is it you are trying to accomplish though?


    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javas cript FAQ - http://jibbering.com/faq/

    Comment

    • Lee

      #3
      Re: insert a script element into the document body from an external script....

      Brian said:[color=blue]
      >
      >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.[/color]

      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.

      Comment

      Working...