javascript 防止 iframe 重定向父页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15578767/
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
prevent iframe from redirecting parent page
提问by aviramtsi
This is the situation: I have on the same domain, an iframe inside my web site. currently, I have no immediate access to the iframe code as of deployment issues.
情况是这样的:我在同一个域中,在我的网站内有一个 iframe。目前,由于部署问题,我无法立即访问 iframe 代码。
when the iframe loads, the code inside it detects something that triggers a redirection on the parent page. Temporarily, I want to remove it. I know I can use the onbeforeload, but a warning will be displayed every time the user click on a any link.
当 iframe 加载时,其中的代码会检测到在父页面上触发重定向的内容。暂时,我想删除它。我知道我可以使用 onbeforeload,但是每次用户单击任何链接时都会显示警告。
Is there a way I can detect the redirection? it happens before the main parent page is fully loaded.
有没有办法检测重定向?它发生在主父页面完全加载之前。
I googled it a lot but still have no working solution.
我用谷歌搜索了很多,但仍然没有可行的解决方案。
Thanks!
谢谢!
回答by Parris
After reading the w3.org spec. I found another solution.
阅读w3.org 规范后。我找到了另一个解决方案。
You can set sandbox=""
, which prevents the iframe frome redirecting. That being said it won't redirect the iframe either. You will lose the click essentially.
您可以设置sandbox=""
,这可以防止 iframe 重定向。话虽如此,它也不会重定向 iframe。您将基本上失去点击。
Example here: http://jsfiddle.net/ppkzS/1/
Example without sandbox: http://jsfiddle.net/ppkzS/
这里的例子:http: //jsfiddle.net/ppkzS/1/
没有沙箱的例子:http: //jsfiddle.net/ppkzS/
回答by Ingo
Try this:
试试这个:
<iframe align="left" src="http://stackoverflow.com/" sandbox=""></iframe>'
回答by metalwings
Use the onbeforeunloadevent to ask the user to leave this page or to stay.
使用onbeforeunload事件要求用户离开此页面或留下。
<script>
window.onbeforeunload = function(){
return false; // This will stop the redirecting.
}
</script>
My answer is based on the answers from answers in Stackoverflow!!!
我的回答基于 Stackoverflow 中的答案!!!
回答by Ben Barden
It's a bit of an ugly hack, but here's something that might work (I admit I don't know a whole lot about the workign of iframes). Grab the page you want to put into the iframe via javascript. Use javascript to parse through it until you get to the redirect code, and strip that redirect code out. Then put the result into the iframe. Ugly at best, but it seems like it might work.
这有点丑陋,但这里有一些可能有用的东西(我承认我对 iframe 的工作标识不太了解)。通过 javascript 抓取您想要放入 iframe 的页面。使用 javascript 解析它,直到到达重定向代码,然后删除该重定向代码。然后将结果放入iframe。充其量是丑陋的,但似乎它可能会起作用。