Monday, February 21, 2011

Manipulating the DOM tree

Hi,

If I load a page in an iframe, run doc.querySelect to retrieve a node N, append N to doc.body by doc.body.appendChild(N), and then remove all children from doc.body until it reaches N, would N be guaranteed to be rendered the same way as pristine in Firefox or IE? So far in the example that I have tried, it's alright, but I was wondering if it'd fail in other settings.

Thanks

From stackoverflow
  • I may be missing something, but why not copy the node, delete the children of doc.body, then append the node to the now empty doc.body? Use the cloneNode() method for copying the node.

    henchman : also a nice idea. don't forget to ignore the iframe while deleting :-)
    s.wong : Sure. But would the browser draw the node the same way as before the manipulation? Would deleting neighbour nodes affect the result of the drawing?
    Robusto : Yes, of course it would. If you are not deleting any parentNodes, from which it might inherit CSS rules, everything should be fine.
  • If you have valid markup and a corresponding doctype that should be fine :-)

0 comments:

Post a Comment