appendChild & cloneNode

C

Chameleon

The following code display a DIV#help with AJAX loaded XML inside.
It works fine in W3C Compliant Browsers but not in IE8.
The problem in IE8 appears in last line.
Is there an error in my code or an IE8 incompatibility?
Any workarounds?
Thanks
---------------------------------------------------------
function openHelp(a) {
var obj = document.getElementById('help');
obj.style.display = 'block';

var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4)
innerDOM(obj, xmlHttp.responseXML.documentElement);
}
xmlHttp.open("GET", 'help.php/' + a, true);
xmlHttp.send(null);
}

function innerDOM(where, root) {
for (var z = 0; z < root.childNodes.length; z++)
where.appendChild(root.childNodes[z].cloneNode(true)); // <-- HERE IS
THE PROBLEM
}
--------------------------------------------------------------
 
D

David Mark

The following code display a DIV#help with AJAX loaded XML inside.
It works fine in W3C Compliant Browsers but not in IE8.
The problem in IE8 appears in last line.
Is there an error in my code or an IE8 incompatibility?
Any workarounds?
Thanks
---------------------------------------------------------
function openHelp(a) {
        var obj = document.getElementById('help');
        obj.style.display = 'block';

        var xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4)
                        innerDOM(obj, xmlHttp.responseXML.documentElement);
        }
        xmlHttp.open("GET", 'help.php/' + a, true);
        xmlHttp.send(null);

}

function innerDOM(where, root) {
        for (var z = 0; z < root.childNodes.length; z++)
                where.appendChild(root.childNodes[z].cloneNode(true));  // <-- HERE IS
THE PROBLEM}

--------------------------------------------------------------

It takes a lot more than that to import XML nodes into an HTML DOM.
It is especially tricky in IE. I suggest you use innerHTML or find
another approach (e.g. download data instead of markup.)
 
S

Stevo

Chameleon said:
The following code display a DIV#help with AJAX loaded XML inside.
It works fine in W3C Compliant Browsers but not in IE8.
The problem in IE8 appears in last line.
Is there an error in my code or an IE8 incompatibility?
Any workarounds?

You haven't told us what error IE8 gives you.
where.appendChild(root.childNodes[z].cloneNode(true)); // <--

Have MS defined where as a reserved word perhaps? Knowing what error IE8
reports would have avoided this silly suggestion ;-)
 
R

RobG

Chameleon wrote: [...]
        where.appendChild(root.childNodes[z].cloneNode(true));    // <--

Have MS defined where as a reserved word perhaps? Knowing what error IE8
reports would have avoided this silly suggestion ;-)

where was declared in the formal parameter list of the function
declaration (which you trimmed). :)
 
N

NickFitz

The following code display a DIV#help with AJAX loaded XML inside.
It works fine in W3C Compliant Browsers but not in IE8.
The problem in IE8 appears in last line.
Is there an error in my code or an IE8 incompatibility?
Any workarounds?
Thanks
---------------------------------------------------------
function openHelp(a) {
        var obj = document.getElementById('help');
        obj.style.display = 'block';

        var xmlHttp = new XMLHttpRequest();
        xmlHttp.onreadystatechange = function() {
                if (xmlHttp.readyState == 4)
                        innerDOM(obj, xmlHttp.responseXML.documentElement);
        }
        xmlHttp.open("GET", 'help.php/' + a, true);
        xmlHttp.send(null);

}

function innerDOM(where, root) {
        for (var z = 0; z < root.childNodes.length; z++)
                where.appendChild(root.childNodes[z].cloneNode(true));  // <-- HERE IS
THE PROBLEM}

--------------------------------------------------------------

Slightly superficial explanation: on Internet Explorer, the
responseXML object is an XML Document containing XML Nodes, whereas
the page into which you are trying to insert the content is an HTML
Document containing HTML Nodes; in the wacky world of Microsoft XML
and HTML have separate and incompatible implementations (even within
the browser), whereas other browsers such as Firefox use one
implementation for both cases.

You are probably better off using the responseText and innerHTML
properties, as David Mark suggests, even though it is less
satisfactory from a purist perspective :-(

Regards,

Nick.
 
R

Richard Cornford

That still won't work on IE. XML nodes, implemented by MSXML,
are a different and incompatible kind of COM component to
the HTML nodes implemented in MSHTML :-(

That would not necessarily matter as the - importNode - method would
be able to create new nodes in the target document based upon the
nodes it received as arguments. Indeed the - importNode - method is
required to create new nodes rather than directly employing its
argument nodes.

Of course the reason that won't work with IE is that Microsoft DOMs
(at very lest the HTML DOM that is the target here) does not have the
- importNode - method implemented.

On the other hand, the existence of an - importNode - method in the
W3C Core DOM does appear to be a fair indicator that the ability to
directly append nodes from one document into another should not be
expected to be available. This impression is reinforced by observing
that the - appendChild - method is allowed/expected to throw a
"WRONG_DOCUMENT_ERR" "if newChild was created from a different
document than the one that created this node". So there is no reason
for expecting to be able to append nodes from an XML document into an
HTML document, even if it is possible to point to some environments
where it can be done.

Richard.
 
J

Joe Kesselman

That still won't work on IE. XML nodes, implemented by MSXML,
That would not necessarily matter as the - importNode - method would
be able to create new nodes in the target document based upon the
nodes it received as arguments. Indeed the - importNode - method is
required to create new nodes rather than directly employing its
argument nodes.

Exactly. importNode()'s job is to read the data from the source node
(using the public DOM APIs) and to recreate equivalent nodes in the
target DOM.
Of course the reason that won't work with IE is that Microsoft DOMs
(at very lest the HTML DOM that is the target here) does not have the
- importNode - method implemented.

.... Which means you have to go back to the solution used before
importNode() was added: Write your own importing subroutine which walks
the source tree and builds equivalent nodes in the target. Easy with
recursion; more difficult but sometimes more efficient with an iterative
engine.
So there is no reason
for expecting to be able to append nodes from an XML document into an
HTML document, even if it is possible to point to some environments
where it can be done.

Or from an XML document to an XML document, unless you know the DOM
implementations can work happily with each other. Basically, if you
haven't written (or examined) the low-level code which created the DOMs,
you'd better plan on importing.

FAQ, as cited.
 
D

David Mark

Exactly. importNode()'s job is to read the data from the source node
(using the public DOM APIs) and to recreate equivalent nodes in the
target DOM.

And it does so poorly (and not at all in IE.)
... Which means you have to go back to the solution used before
importNode() was added: Write your own importing subroutine which walks
the source tree and builds equivalent nodes in the target. Easy with
recursion; more difficult but sometimes more efficient with an iterative
engine.

It's been done to death. All a waste of time. Use innerHTML or JSON.

[snip]
 
R

RobG

JSON ? What's JSON ?

Oh well, I'll see it in the good old great cljs FAQ :)

http://www.google.com/search?&q=site:jibbering.com/faq/+json

Ooops:

"Your search - site:jibbering.com/faq/ json - did not match any
documents."

Not surprising, there aren't any frequently asked questions about JSON
here.

Pathetic, pitiful...

Not at all, quite reasonable in fact. An FAQ is for *frequently*
asked questions, I don't see frequent questions about JSON here. If
the purpose of the clj FAQ was general javascript topics, it would be
perhaps 50 times larger that it is, at least.

In any case, if you think an item on JSON is required, write one and
post it here for review and possible inclusion in the FAQ.
 
J

Jorge

(...)
In any case, if you think an item on JSON is required, write one and
post it here for review and possible inclusion in the FAQ.

Again ? Ok, here it goes:

-----------------------------------------------------------------------
FAQ Topic - What online resources are available?
-----------------------------------------------------------------------

http://crockford.com/javascript/

JSON: http://www.JSON.org/
JSLINT: http://www.JSLint.com/
JSMIN: http://javascript.crockford.com/jsmin.html
JSMIN: http://fmarcia.info/jsmin/test.html
(...)

HTH,
 
G

Garrett Smith

Garrett said:
Jorge said:
On Jun 2, 10:11 pm, David Mark <[email protected]> wrote:
[...]
"Your search - site:jibbering.com/faq/ json - did not match any
documents."

Pathetic, pitiful...

Yes, and LOL funny.

Make sure that you type the search term criteria first, before the site
filter.

Like this:-
json site:jibbering.com/

It does seem to work the other way, too. Guess it isn't funny after all.
Just annoying.

Garrett
 
G

Garrett Smith

RobG said:
On Jun 2, 10:11 pm, David Mark <[email protected]> wrote:
[...]


Not at all, quite reasonable in fact. An FAQ is for *frequently*
asked questions, I don't see frequent questions about JSON here. If
the purpose of the clj FAQ was general javascript topics, it would be
perhaps 50 times larger that it is, at least.

In any case, if you think an item on JSON is required, write one and
post it here for review and possible inclusion in the FAQ.

As stated in the "comments and suggestions" entry:

| A Draft Proposal for the FAQ is requested and appreciated.

Garrett
 
J

Jorge

(...)
Make sure that you type the search term criteria first, before the site
filter.

Like this:-
json site:jibbering.com/
(...)

Oh, how clever ! But that doesn't restrict the search to the FAQ...
and in fact, none of the results are in the FAQ. So, still, pathetic
and pitiful.

Thanks,
 
J

Jorge

(...)
As stated in the "comments and suggestions" entry:

| A Draft Proposal for the FAQ is requested and appreciated.

Requested might me, but obviously not much appreciated.

Just in case, here it goes: my proposal (again): ADD THIS, THANKS :


-----------------------------------------------------------------------
FAQ Topic - What online resources are available?
-----------------------------------------------------------------------

(NEW) VIDEO: Changes to JavaScript, Part 1: EcmaScript 5
Presented by Mark Miller, Waldemar Horwat, and Mike Samuel.

http://crockford.com/javascript/

JSON: http://www.JSON.org/
JSLINT: http://www.JSLint.com/
JSMIN: http://javascript.crockford.com/jsmin.html
JSMIN: http://fmarcia.info/jsmin/test.html

VIDEOS about JavaScript:
http://developer.yahoo.com/yui/theater/ :

The JavaScript programming language:
http://video.yahoo.com/video/play?vid=111593
http://video.yahoo.com/video/play?vid=111594
http://video.yahoo.com/video/play?vid=111595
http://video.yahoo.com/video/play?vid=111596

Advanced JavaScript:
http://video.yahoo.com/video/play?vid=111585
http://video.yahoo.com/video/play?vid=111586
http://video.yahoo.com/video/play?vid=111587

An Inconvenient API: The Theory of the DOM:
http://video.yahoo.com/video/play?vid=111582
http://video.yahoo.com/video/play?vid=111583
http://video.yahoo.com/video/play?vid=111584

JavaScript: The Good Stuff:
http://video.yahoo.com/video/play?vid=630959

The State of Ajax:
http://yuiblog.com/blog/2007/11/06/video-crockford

Web Forward:
http://video.yahoo.com/watch/3730137/10250950

Ajax Performance:
http://video.yahoo.com/watch/4141759/11157560

Gears and the mashup problem:
http://video.google.com/videoplay?docid=452089494323007214

JavaScript: The Good Parts. (VIDEO)
 
J

Jorge

(...)
Yes, and LOL funny.
(...)

LOL and funny ? That wasn't supposed to be funny, or did you see any
smiley ? That's pathetic and pitiful for a *JAVASCRIPT* FAQ...
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top