javascript - window.opener.close()无效
问题描述
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’ 却可以,请问是什么原因
问题解答
回答1:close 方法只能关闭由自己打开的window
回答3:<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();
可以关掉子窗口,在子窗口中无效。
相关文章:
1. css - 新手做响应式布局, 断点过后右侧出现空白,求帮助,谢谢。2. javascript - 关于<a>元素与<input>元素的JS事件运行问题3. python - 启动Eric6时报错:’qscintilla_zh_CN’ could not be loaded4. mysql - 查询字段做了索引为什么不起效,还有查询一个月的时候数据都是全部出来的,如果分拆3次的话就没问题,为什么呢。5. mysql - 记得以前在哪里看过一个估算时间的网站6. ID主键不是自增的吗 为什么还要加null7. 大家好,我想请问一下怎么做搜索栏能够搜索到自己网站的内容。8. MySQL中的enum类型有什么优点?9. css3 - 纯css实现点击特效10. javascript - ES6 中能否把 class 属性 方法 分文件?
