473,385 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 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)");
}
================================================
Jul 23 '05 #1
2 9059
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/
Jul 23 '05 #2
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.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Akbar | last post by:
Hey there, Big-time curiosity issue here... Here's the test code (it's not that long)... it's to display a large number of image links with captions, ideally pulled in from an external file...
8
by: Johnny Knoxville | last post by:
I've added a favicon to my site (http://lazyape.filetap.com/) which works fine if you add the site to favourites the normal way, but I have some JavaScript code on a couple of pages with a link,...
2
by: Brian | last post by:
I'm trying out a script called mktree.js < http://www.mattkruse.com/javascript/mktree/ > I want to add an expand all button to the script, using the DOM insertBefore method. I added a function to...
4
by: Sergio del Amo | last post by:
i, I have the next html page <html> <head> <script> <!-- function insertcode() { var code ="<p> blablabal babala babababab</p><h1>here comes header</h1><span>fadfafa<a...
4
by: Jake j | last post by:
To those of you who sent me working examples of js include routines, much thanks. I see from them examples that what I'm trying to get to requires a higher level of js knowledge than I've got. ...
2
by: . . | last post by:
Hi I need some help with java script . I have a ASPX page that is pulling a user message heading from the table on the page . The user message header has message body which suppose to dispay...
3
by: Water Cooler v2 | last post by:
Questions: 1. Can there be more than a single script block in a given HEAD tag? 2. Can there be more than a single script block in a given BODY tag? To test, I tried the following code. None...
14
RMWChaos
by: RMWChaos | last post by:
Firebug is reporting "too much recursion" when I attempt to create a child element in a parent that doesn't exist yet. The script should automatically create the missing parent before going on to...
2
by: Jay | last post by:
I have a web app running on the windows CE device. In one of the asp.net pages - it has javascript code. That seems to have a memory leak. When I run the web app - in about one hour, the app hangs....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.