Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   appendChild & cloneNode (http://www.velocityreviews.com/forums/t686331-appendchild-and-clonenode.html)

Chameleon 06-01-2009 09:35 PM

appendChild & cloneNode
 
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
}
--------------------------------------------------------------

David Mark 06-01-2009 09:50 PM

Re: appendChild & cloneNode
 
On Jun 1, 5:35*pm, Chameleon <cham_...@hotmail.com> wrote:
> 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.)

Stevo 06-01-2009 10:24 PM

Re: appendChild & cloneNode
 
Chameleon wrote:
> 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 ;-)

RobG 06-01-2009 10:58 PM

Re: appendChild & cloneNode
 
On Jun 2, 8:24*am, Stevo <n...@mail.invalid> wrote:
> 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). :-)


--
Rob

NickFitz 06-02-2009 09:57 AM

Re: appendChild & cloneNode
 
On Jun 1, 10:35*pm, Chameleon <cham_...@hotmail.com> wrote:
> 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.
--
Nick Fitzsimons
http://www.nickfitz.co.uk/

Joe Kesselman 06-02-2009 12:31 PM

Re: appendChild & cloneNode
 
When nodes come from different documents, you should probably be using
the importNode operation.

http://www.w3.org/DOM/faq.html#ownerdoc

NickFitz 06-02-2009 01:24 PM

Re: appendChild & cloneNode
 
On Jun 2, 1:31*pm, Joe Kesselman <keshlam.cat.nos...@verizon.net>
wrote:
> When nodes come from different documents, you should probably be using
> the importNode operation.
>
> http://www.w3.org/DOM/faq.html#ownerdoc


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 :-(

Richard Cornford 06-02-2009 03:41 PM

Re: appendChild & cloneNode
 
On Jun 2, 2:24 pm, NickFitz wrote:
> On Jun 2, 1:31 pm, Joe Kesselman wrote:
>
>> When nodes come from different documents, you should
>> probably be using the importNode operation.

>
>> http://www.w3.org/DOM/faq.html#ownerdoc

>
> 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.

Joe Kesselman 06-02-2009 08:01 PM

Re: appendChild & cloneNode
 
>> 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.


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.

David Mark 06-02-2009 08:11 PM

Re: appendChild & cloneNode
 
On Jun 2, 4:01*pm, Joe Kesselman <keshlam.cat.nos...@verizon.net>
wrote:
> >> 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.

>
> 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.)

>
> > 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.


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

[snip]


All times are GMT. The time now is 12:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.