Check if document is ROOT node
I need to know if the document element is the ROOT node of the page. For example:
<html> <-- ROOT Node
<head></head>
<body>
<iframe>
<html>...</html> <-- other document
</iframe>
<iframe>
<html>...</html> <-- other document
</iframe>
</body>
</html>
Javascript that is executed in iframe 1 or 2 should know if their document node is the root node开发者_开发百科.
Hope you can help me.
You should be able to do this with top
:
if (window.top.document === window.document) {
// we're in the outermost window
}
I suspect that, given the contents of an are different documents, they'll all report back as the root node. You might be better to check to see if document.parent
is null or not.
if (window == window.parent) {
alert("I'm not in a frame");
}
Make a function in your top document that returns it's rootNode, then call this function from your iframe documents by using the window.top reference:
in your top document:
function getRootNode()
{
//returns the rootNode
}
In your iframe documents:
var rootNode = window.top.document.getRootNode();
try out this :
if(currentnode.parentNode == null) { alert("is root node") }
// where currentnode is the node which you'll select
精彩评论