Reputation: 1459
I have tried many properties of window to see if a page in an iframe can tell if it is in an iframe. I have tried:
if(top.location!= self.location) //doesn't work in Google Chrome
alert("I am in an iframe!")
And this doesn't work (works on all browsers but Chrome). I am writing a userscript for Firefox and Chrome but Chrome really doesn't behave. Is there a way to tell if Chrome can detect if its page is in an iframe?
Upvotes: 7
Views: 5393
Reputation: 1597
window.frameElement
is supported in even the most ancient browsers. It identifies an iframe, embed, or object in which the current window is embedded.
if(!!window.frameElement){
//code to be executed if we are in an iframe
}
Upvotes: 0
Reputation: 86882
This works for frames I would assume it also works with iFrames
if (top === self) {
// no frame
} else {
//frame
}
Upvotes: 15