Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > insert a script element into the document body from an external script....

Reply
Thread Tools

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

 
 
Brian
Guest
Posts: n/a
 
      06-24-2004
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)");
}
================================================
 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      06-24-2004
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/
 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      06-24-2004
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
document or document.body Martin Rinehart Javascript 13 12-07-2008 01:06 PM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
document.body.scrollTop for an "autoscroll: auto" css element Jefferds.A@gmail.com Javascript 1 07-29-2006 02:54 PM
Is there a way to insert an existing htm page into body of a mailmessage? Mark Sandfox ASP .Net 2 06-24-2004 02:00 PM
How to insert dynamically generated HTML into the <body> of an ASP.NET page? Rick Spiewak ASP .Net 5 08-01-2003 12:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57