检测iframe是跨域的,然后突出它 [英] Detect when iframe is cross-domain, then bust out of it
问题描述
我有一个包含大部分内容的大型iframe的网页。用户通过在iframe中点击来与网站进行互动。我想要建立的功能是:当用户离开我的网站,我做他们的支持和淘汰的iframe。
I have a page with a large iframe that contains a majority of the content. The user interacts with the website by clicking around within the iframe. The functionality I'm trying to build is: When a user navigates away from my site, I do them a favor and bust out of the iframe.
iframe有一个
<iframe id="testframe" src="http://mysite.com" onload="testframe_loaded()"></iframe>
每次事件触发时,我都在寻找一些方法:
Each time the event is fired, I'm looking for some way to:
A )当用户导航到其他域时检测
A) Detect when the user navigates to a different domain
B
B) Bust out of the iframe.
我怀疑B不可能,因为浏览器不能访问
I suspect that B isn't possible, since browsers don't give access to
document.getElementById("testframe").contentDocument.location.href
iframe是跨域。我也不确定A是否可能。
when the iframe is cross-domain. I'm also not sure whether or not A is possible.
如果任何人有想法如何完成这一点,或者是积极的,它是不可能做到的,
If anybody has ideas as to how to accomplish this, or is positive that it can't be done, I'd appreciate the advice.
感谢
推荐答案
do A,因为如果你得到一个安全错误(你可以捕获),这意味着他们是跨域:)然后你可以调整大小的iframe是整个页面。但我认为你是对的,你不能实际上爆发,没有明确的合作从其他领域。
You can do A, since if you get a security error (which you can catch), that's means they're cross-domain :) Then you can resize the iframe to be the whole page. But I think you're right you can't actually bust out, without explicit cooperation from the other domain.
编辑:你是对的,你不需要轮询。以下是基本代码:
You're right you don't need to poll. Here's the basic code:
<html>
<head>
<title>Cross-domain frame test</title>
<!-- http://stackoverflow.com/questions/2365822/detect-when-iframe-is-cross-domain-then-bust-out-of-it/2365853#2365853 -->
<script type="text/javascript">
function checkForCross()
{
var iframe = document.getElementById("myFrame");
try
{
var loc = iframe.contentDocument.location.href;
} catch(e)
{
iframe.setAttribute("style", "border: 0; margin: 0; padding: 0; height: 100%; width: 100%;");
}
}
</script>
</head>
<body>
<iframe name="myFrame" id="myFrame" src="child.html" style="height: 50%; width: 50%;" onload="checkForCross()"></iframe>
</body>
</html>
这篇关于检测iframe是跨域的,然后突出它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!