简体   繁体   English

阻止iframe重定向父页面

[英]prevent iframe from redirecting parent page

This is the situation: I have on the same domain, an iframe inside my web site. 情况就是这样:我在同一个域中,在我的网站内有一个iframe。 currently, I have no immediate access to the iframe code as of deployment issues. 目前,我无法立即访问iframe代码,因为部署问题。

when the iframe loads, the code inside it detects something that triggers a redirection on the parent page. 当iframe加载时,其中的代码会检测到在父页面上触发重定向的内容。 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. 我知道我可以使用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. 我google了很多,但仍然没有工作的解决方案。

Thanks! 谢谢!

After reading the w3.org spec . 阅读完w3.org规范后 I found another solution. 我发现了另一个解决方

You can set sandbox="" , which prevents the iframe frome redirecting. 您可以设置sandbox="" ,这可以防止iframe重定向。 That being said it won't redirect the iframe either. 据说它也不会重定向iframe。 You will lose the click essentially. 你基本上会失去点击。

Example here: http://jsfiddle.net/ppkzS/1/ 示例: http//jsfiddle.net/ppkzS/1/
Example without sandbox: http://jsfiddle.net/ppkzS/ 没有沙箱的示例: http//jsfiddle.net/ppkzS/

尝试这个:

<iframe align="left" src="http://stackoverflow.com/" sandbox=""></iframe>'

Use the onbeforeunload event 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答案的答案!

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). 这是一个丑陋的黑客,但这里可能有用(我承认我不太了解iframe的工作原理)。 Grab the page you want to put into the iframe via javascript. 通过javascript抓取您要放入iframe的页面。 Use javascript to parse through it until you get to the redirect code, and strip that redirect code out. 使用javascript解析它直到你到达重定向代码,并删除重定向代码。 Then put the result into the iframe. 然后将结果放入iframe。 Ugly at best, but it seems like it might work. 最好是丑陋,但似乎它可能会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM