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. docker start -a dockername 老是卡住,什么情况?2. python运行后没有任何反馈要怎么排查3. java内存模型的happens-before语义顺序问题4. java - 配置tomcat免安装版,无法启动 返回错误码45. 请问下在VueJS中v-for 怎么拿到循环次数的变量呢?6. javascript - QQ自带浏览器中,点击返回,就刷新页面,关闭按钮不出现,有没有人知道是怎么回事???7. Mysql 关于 FOUND_ROWS() 和 ROW_COUNT() 函数8. logging - Python日志, 时间应该使用UTC时间还是本地时间?9. 鼠标过导航标签时,无效果,请问如何查找问题10. html5 - 现在做了个宣传用的H5需要获取openid
