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

B

Brian

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)");
}
================================================
 
R

Randy Webb

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.

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?
 
L

Lee

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,997
Latest member
mileyka

Latest Threads

Top