Reputation: 679
I have a few questions (I am interested in answers related to Firefox, but answers in context of other browser might be acceptable too).
Do DOM Mutation events get fired for nodes disconnected from the main document, i.e. are disconnected? As per information present in one of the Mozilla bugs, the answer seems yes. Can someone give me some examples?
What is the quickest way of finding if a node is disconnected or not? The naive approach is walk (using node.parentNode) all the way up till we reach null or the document. Anything faster?
Does Firefox support Webkit's 'magic iframe' feature? If not, what will happen if that code ran in Firefox?
Related to #3, is it possible for a iframe to continue loading while disconnected from the document? I.e. it was connected to a main doc, loading started, then moved to another document (using adoptNode()), but never added to a parent node in the new doc. Will iframe continue loading?
Thanks in advance, Sunil
Upvotes: 1
Views: 425
Reputation: 349252
The DOMNodeRemoved
event is fired when an a node (element, text node, comment, ..) is removed from the document/element to which the event is bound.
document.addEventListener('DOMNodeRemoved', function(event) {
console.log('Removed node: ' + event.target.nodeName);
});
Benchmarking two possibilities:
A plain loop:
function isDisconnected(node) {
var rootElement = document.documentElement;
while (node) {
if (node === rootElement)
return false;
node = node.parentNode;
}
return true;
}
function isDisconnected(node) {
return !document.contains(node);
}
Result: http://jsperf.com/dom-contains-vs-parentnode
The document.contains()
method significantly (47x) faster than the loop method (whether the nodes are disconnected or not, the document.contains
method always outperforms the loop).
The provided jsfiddle moves an iframe whose src
attribute is set to "data:text/html,<script>alert(/Test/);<\/script>"
. When this URL is loaded, an alert shows up. Two alerts showing up means that the Magic iframe feature is not supported. One alert showing up means that the feature is supported. Zero alerts showing up means that your JavaScript is disabled, or that your browser doesn't support data-URIs.
Upvotes: 1