Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if page is in Iframe for Google Chrome

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?

like image 353
user654628 Avatar asked Mar 11 '11 21:03

user654628


People also ask

How do you tell if a page is in an iframe?

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.

How do I find iFrames in Google Chrome?

We can detect if an element is inside an iframe by inspecting the element with the Chrome Developer Tools. An easier way is to perform a Right Click near the element in your browser and see if View Frame Source option is present in the context dropdown.

Does Google Chrome support iFrames?

iFrame is not working in Chrome but works in Firefox Google Chrome has a different set of rules when it comes to iFrame and it often blocks the content although it works fine on other browsers. You should also try clicking the shield in the URL title bar because that would also help you see the content.


1 Answers

This works for frames I would assume it also works with iFrames

if (top === self) { 
  // no frame
} else { 
  //frame 
}
like image 86
John Hartsock Avatar answered Sep 17 '22 18:09

John Hartsock