检查我的网页是否嵌入在iframe中 [英] Checking if my page is embedded in an iframe

查看:126
本文介绍了检查我的网页是否嵌入在iframe中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试我的页面(php)是否嵌入在iframe中,以便实现不同的行为。知道如何测试这个。如果有帮助,我也会使用jQuery。

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.

添加:
我特别感兴趣的是如果有方法可以使用Javascript在服务器而不是客户端上测试这个

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

推荐答案

您可以使用JavaScript,我认为以下内容应该可行:

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

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

链接到原创,meyerweb,文章

< hr />
已编辑关于问题的更新:


添加:我特别感兴趣的是,如果有一种方法可以在服务器上测试,而不是在客户端使用Javascript

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

这不能在服务器端检查,但是,可以使用 X-Frame-Options 标题,有两个选项:

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


  1. DENY :防止资源被绑定到任何地方(假设浏览器支持X-Frame-Options标题,或者

  2. SAMEORIGIN :它允许仅通过s中的页面来构建资源ame-domain,非常类似于JavaScript的同源政策。

  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.

要使用此功能,您需要配置服务器以发送相关标题;虽然在不知道你正在运行什么服务器的情况下无法给出具体的建议;虽然 Mozilla开发人员中心上的链接文章确实显示了Apache选项。

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.

这篇关于检查我的网页是否嵌入在iframe中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆