javascript - window.opener.close()无效
PHP中文网
PHP中文网 2017-06-26 10:52:27
[JavaScript讨论组]

1.父页面window.open()打开新页面

var targetWeb=null;
if(targetWeb){
    targetWeb.focus();
}else{
    targetWeb=window.open('https://segmentfault.com','segmentfault');
}

2.子页面中关闭父页面

window.opener.close();

发现子页面无法关闭父页面,会提示:Scripts may close only the windows that were opened by it
但若换成:window.opener.location.href='https://www.hao123.com' 却可以,请问是什么原因

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(3)
phpcn_u1582

大家讲道理

close 方法只能关闭由自己打开的window

黄舟

<html>
<body>

<script type="text/javascript">
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("This is 'myWindow'");
myWindow.document.write("<script>window.opener.close()</script>");
myWindow.focus();
myWindow.opener.document.write("This is the parent window");
myWindow.close();
</script>

</body>
</html>


尝试了这段代码,在父窗口中直接使用
 myWindow=window.open('','','width=200,height=100');
    myWindow.close();

可以关掉子窗口,在子窗口中无效。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号