javascript – 如何绕过window.opener跨域安全性
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 如何绕过window.opener跨域安全性,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2637字,纯文字阅读大概需要4分钟。
内容图文
![javascript – 如何绕过window.opener跨域安全性](/upload/InfoBanner/zyjiaocheng/714/4e89d639eed649b8a71719298f2f386d.jpg)
我刚刚发现window.opener在通过window.open打开的窗口中不可用,如果新的URL是跨域的,在IE中. How do I detect window opener in IE
如果窗口在我的域中启动,离开它,然后返回到我的域,则会发生这种情况.我试图在弹出窗口中进行社交注册(facebook,google等).完成后应该关闭新窗口并重定向开启器.
我知道Soundcloud正在关闭它,但我不知道如何.我看到URL从他们改为Facebook,然后关闭.
从第三方重定向回我的网站后,我运行:
var data = {
type : 'complete',
destination : '<?= $destination; ?>'
};
if ( window.opener ) {
window.opener.postMessage( JSON.stringify( data ), '*' );
window.close();
}
else {
alert( "Unable to find window" );
}
它在IE中提醒,即使窗口最初是我的域,然后重定向到FB,然后重定向回到我.我想可能因为我打开我的网站并立即从PHP重定向可能是一个问题.然而,即使我打开我的网站,window.location.href =’facebookssite.com’仍然在返回时抱怨.
注意
社交注册不适用于iframe中的Google,FB等.我相信他们出于安全原因不允许他们.
解决方法:
反过来做.从主(开启者)窗口跟踪子弹出窗口的状态,您可以很容易地知道子窗口何时被导航回您的域,因此您可以再次与其“对话”.但是不要单独关闭子窗口.让开启窗口从子窗口获取结果然后关闭它.
例如,main.html:
<!DOCTYPE html>
<head>
<title>main</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<script>
window.addEventListener("message", function(ev) {
if (ev.data.message === "deliverResult") {
alert("result: " + ev.data.result);
ev.source.close();
}
});
function Go() {
var child = window.open("child.html", "_blank", "height=200,width=200");
var leftDomain = false;
var interval = setInterval(function() {
try {
if (child.document.domain === document.domain)
{
if (leftDomain && child.document.readyState === "complete")
{
// we're here when the child window returned to our domain
clearInterval(interval);
alert("returned: " + child.document.URL);
child.postMessage({ message: "requestResult" }, "*");
}
}
else {
// this code should never be reached,
// as the x-site security check throws
// but just in case
leftDomain = true;
}
}
catch(e) {
// we're here when the child window has been navigated away or closed
if (child.closed) {
clearInterval(interval);
alert("closed");
return;
}
// navigated to another domain
leftDomain = true;
}
}, 500);
}
</script>
</head>
<body>
<button onclick="Go()">Go</button>
</body>
child.html:
<!DOCTYPE html>
<head>
<title>child</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<script>
window.addEventListener("message", function(ev) {
if (ev.data.message === "requestResult") {
// ev.source is the opener
ev.source.postMessage({ message: "deliverResult", result: true }, "*");
}
});
</script>
</head>
<body>
<a href="http://www.example.com">Go to example.com</a>
Then click the browser Back button when ready.
</body>
用IE10测试.
内容总结
以上是互联网集市为您收集整理的javascript – 如何绕过window.opener跨域安全性全部内容,希望文章能够帮你解决javascript – 如何绕过window.opener跨域安全性所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。