Accessing copy of a portion of HTML from JavaScript

J

John L.

I'd like to access a copy of a portion of my HTML document from
JavaScript contained within the HTML. I thought this would work:

var myHeader = ((Node)
(document.getElementById("header"))).cloneNode(true);

with the following HTML contained in the BODY section:

<DIV id="header">
o o o (various HTML statements)
</DIV>

However, I receive this error with Internet Explorer attempting to
view the HTML page:

Object Expected

The Opera browser was a little more helpful.It reported:

Error:
Unhandled exception: [Object DOMException]
code: 9
message: NOT_SUPPORTED_ERR

I suspect it is casting the Element as a Node that is failing, as the
Element interface extends the Node interface. How can I accomplish the
same functionality?

Thanks in advance for any help you may be able to provide.


John L.
 
S

SAM

John L. a écrit :
I'd like to access a copy of a portion of my HTML document from
JavaScript contained within the HTML. I thought this would work:

var myHeader = ((Node)
(document.getElementById("header"))).cloneNode(true);

with the following HTML contained in the BODY section:

<DIV id="header">
o o o (various HTML statements)
</DIV>

However, I receive this error with Internet Explorer attempting to
view the HTML page:

following works for me :

function cloneDiv() {
var clone = document.getElementById('header').cloneNode(true);
clone.id = 'foo';
document.body.appendChild(clone);
}
window.onload = cloneDiv;

Thanks in advance for any help you may be able to provide.

do you clone *after* the page is loaded ?
 
T

Thomas 'PointedEars' Lahn

John said:
I'd like to access a copy of a portion of my HTML document from
JavaScript contained within the HTML. I thought this would work:

var myHeader = ((Node)
(document.getElementById("header"))).cloneNode(true);

with the following HTML contained in the BODY section:

<DIV id="header">
o o o (various HTML statements)

The HyperText *Markup Language* is not a programming language; it does not
have statements.
</DIV>

However, I receive this error [...]

Java != JavaScript. Current client-side ECMAScript implementations do not
support strict typing and associated Java-style typecasting, nor would that
be required here. D::gEBI() returns a reference to a DOM object that
implements the HTMLDivElement interface of W3C DOM Level 2 HTML (DOM2HTML)
here which inherits from the Node interface of W3C DOM Level 2 Core
(DOM2Core) through the DOM2HTML:HTMLElement and DOM2Core:Element interfaces,
respectively:

http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-22445964

Correct syntax:

var myHeader = document.getElementById("header").cloneNode(true);


HTH

PointedEars
 
P

pr

John said:
var myHeader = ((Node)
(document.getElementById("header"))).cloneNode(true);

There is an 'ECMAScript Language Binding' appendix at the back of many
W3C specs, which should help with any syntax trouble.
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top