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. android - 如何缩小APK的体积2. vue.js - vue 打包后 nginx 服务端API请求跨域问题无法解决。3. dockerfile - 我用docker build的时候出现下边问题 麻烦帮我看一下4. angular.js - angular post的Content-Type被设置,导致不能上传图片,求助!!5. position:absolute、float、display:inline-block 都能实现相同效果,区别是什么?6. angular.js - Web应用,单页面应用Cache问题7. 图片上传成功但数据库字段是空8. css3 - 这个效果用 CSS 可以实现吗?border-image9. android - webview 自定义加载进度条10. javascript - vue更改当前节点元素

网公网安备