Check if my page is included in iframe

I would like to check if my page (php) is included in the iframe or not in order to implement a different behavior. Any idea how to check this out. I also use jQuery if this helps.

Addition: I am particularly interested in if there was a way to check this on the server, and not on the client with Javascript

+4
source share
4 answers

You can use JavaScript, I think something like the following should work:

if (top != self) { // you're in an iframe, or similar. } 

Link to the original, meyerweb, article .


Edited regarding updating the question:

Addition : I am particularly interested if there was a method for checking this on the server, and not on the client with Javascript

This cannot be "checked" on the server side, but you can use the X-Frame-Options header, there are two options

  • DENY : prevents the creation of a frame anywhere (provided that the browser supports the X-Frame-Options header) or
  • SAMEORIGIN : this allows you to create a resource using only pages from the same domain, similar to a JavaScript policy with the same source code.

To use this, you need to configure the server to send the appropriate header; although specific recommendations for this cannot be given without knowing which server you are working on; although a related article in the Mozilla Developer Center shows the Apache option.

+12
source

perhaps:

 var isInIFrame = (window.location != window.parent.location) ? true : false; 
+4
source

I don't know if there is any specific jQueryway, but in javascript for vanilla you can simply:

 if (top != self) alert("framed!") 
+3
source
 <script language="JavaScript" type="text/javascript"> function InFrame() { if (top.location != location) { //Do whatever you need- your site is in an iframe. //This will redirect to your site if you need to //top.location.href = document.location.href ; // } } </script> 
0
source

All Articles