Html 带有外部页面的 iframe 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9158024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iframe with external page not working
提问by Moshe Shaham
I'm tring to embed a webpage in an iframe, but it doesn't work at all. internal pages with relative path are displayed normally. but this simple code doesn't work:
我想在 iframe 中嵌入一个网页,但它根本不起作用。有相对路径的内部页面正常显示。但是这个简单的代码不起作用:
<iframe src="http://www.google.com/"></iframe>
the place that supposed to show the iframe is just empty. i looked in the page source and there is nothing after
应该显示 iframe 的地方只是空的。我查看了页面源代码,之后什么也没有
How can this be?
怎么会这样?
回答by Daan
Google uses an X-FRAME-OPTIONS HTTP header to disallow putting their pages in iframes: https://developer.mozilla.org/en/The_X-FRAME-OPTIONS_response_header
Google 使用 X-FRAME-OPTIONS HTTP 标头禁止将其页面放入 iframe:https: //developer.mozilla.org/en/The_X-FRAME-OPTIONS_response_header
Almost all modern browsers will refuse to put pages with this HTTP header in an iframe. There's nothing you can do about that.
几乎所有现代浏览器都拒绝将带有此 HTTP 标头的页面放入 iframe。对此你无能为力。
回答by horsley
Because the internal page had do something to prevent to be put in iframe.
因为内部页面已经做了一些事情来防止被放入iframe。
Maybe a piece of javascript like that
也许像那样的一段 javascript
if (window.top != window.self) {window.top.location = window.self.location;}
回答by Sachin Pundir
Suppose your url is www.google.com, i.e $url = "www.google.com";
假设你的网址是 www.google.com,即 $url = "www.google.com";
$headerRes = get_headers($url); //get the header response
foreach($headerRes as $val)
if($val=="X-Frame-Options: SAMEORIGIN" || $val=="X-Frame-Options: DENY"){
header("location:".$url);
exit;
}
//simply redirect to their website instead of showing blank frame
I hope I explained myself good.
我希望我能很好地解释自己。