Check to see if a DOM node is inside an Iframe

I would like to check to see if a DOM Node is inside an iFrame in my application. Is there anyway to find what window the node resides in?

Answers:

Answer
if (node.ownerDocument.defaultView.frameElement)        
    // We're inside of an <iframe>

Explantions to the code:

node.ownerDocument.defaultView.frameElement
//   ^------------------------------------------ The document of the <iframe>.
//                 ^ --------------------------- The window of the <iframe>
//                             ^---------------- The <iframe> 

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.