Emm*_*ett 12 javascript security iframe cross-domain
我有一个包含大量iframe的页面,其中包含大部分内容.用户通过在iframe中单击来与网站进行交互.我正在尝试构建的功能是:当用户离开我的网站时,我帮他们做了一个忙,然后从iframe中删除.
iframe有一个onload事件,每次加载新页面时都会触发该事件,跨域或不是.
<iframe id="testframe" src="http://mysite.com" onload="testframe_loaded()"></iframe>
Run Code Online (Sandbox Code Playgroud)
每次事件被触发时,我都在寻找一些方法:
A)检测用户何时导航到不同的域
B)吹出iframe.
我怀疑B是不可能的,因为浏览器不提供访问权限
document.getElementById("testframe").contentDocument.location.href
Run Code Online (Sandbox Code Playgroud)
当iframe是跨域的时候.我也不确定A是否可行.
如果有人有关于如何实现这一目标的想法,或者对于无法做到这一点是积极的,我会很感激这些建议.
谢谢
您可以执行A,因为如果您遇到安全性错误(您可以捕获),这意味着它们是跨域的:)然后您可以将iframe的大小调整为整个页面.但是,如果没有来自其他领域的明确合作,我认为你是对的,你实际上不能破产.
编辑:你是对的,你不需要民意调查.这是基本代码:
<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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10885 次 |
最近记录: |