In short, to check if a page is in an iframe, you need to compare the object's location with the window object's parent location. If they are equal, then the page is not in an iframe; otherwise, a page is in an iframe.
To check if iframe is loaded or it has a content with JavaScript, we can set the iframe's onload property to a function that runs when the iframe is loaded. document. querySelector("iframe"). onload = () => { console.
The X-Frame-Options HTTP response header can be used to indicate whether or not a browser should be allowed to render a page in a <frame> , <iframe> or <object> . Sites can use this to avoid clickjacking attacks, by ensuring that their content is not embedded into other sites.
Looking at frame length breaks down generally if page A itself has frames (I know this might not be the case for this specific instance). The more reliable and meaningful test would be:
if (window!=window.top) { /* I'm in a frame! */ }
The predicate
(window.parent.frames.length > 0)
will tell you just what you want.
if (top === self) { not in a frame } else { in a frame }
From How to identify if a webpage is being loaded inside an iframe or directly into the browser window?
As stated above the accepted solution doesn't work in IE8. Additionally, checking window.parent.frames.length
can cause a cross-domain exception.
Instead I was able to achieve this with var isInIFrame = top.location != self.location
- it works in IE8 and it doesn't cause a cross-domain violation as long as you don't attempt to read the contents of top.location
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With