5

I would like to test if my page (php) is embedded in an iframe or not, in order to implement a different behaviour. Any idea how to test this. I'm also using jQuery if it helps.

Addition : I'm especially interested if there would be a method to test this on the server rather than in the client with Javascript

asked May 2, 2011 at 15:59
5

4 Answers 4

13

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

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

Link to original, meyerweb, article.


Edited with regard to the question's update:

Addition : I'm especially interested if there would be a method to test this on the server rather than in the client with Javascript

This can't be 'checked' on the server side, but, you could use the X-Frame-Options header, there are two options:

  1. DENY: prevents the resource being framed anywhere (assuming the browser supports the X-Frame-Options header, anyway), or
  2. SAMEORIGIN: which allows framing of the resource only by pages from the same-domain, much like JavaScript's same-origin policy.

To use this, you'd need to configure your server to send the relevant header; though specific advice for that can't be given without knowing what server you're running; though the linked article at the Mozilla Developer Center does show the Apache option.

answered May 2, 2011 at 16:02
Sign up to request clarification or add additional context in comments.

Comments

4

maybe:

var isInIFrame = (window.location != window.parent.location) ? true : false;
answered May 2, 2011 at 16:01

2 Comments

What do you need ? true : false for?
Maybe he doesn't know that he can set a value like this var isInIFrame = (window.location != window.parent.location); anyway, its the samething
3

I don't know if there is a specific JQueryway but in vanilla javascript you can simply;

if (top != self)
 alert("framed!")
answered May 2, 2011 at 16:03

Comments

0
<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>
answered May 2, 2011 at 16:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.