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/