1. Parent page window.open() opens a new page
var targetWeb=null;
if(targetWeb){
targetWeb.focus();
}else{
targetWeb=window.open('https://segmentfault.com','segmentfault');
}
2. Close the parent page in the child page
window.opener.close();
If it is found that the child page cannot close the parent page, it will prompt: Scripts may close only the windows that were opened by it
But if it is replaced with: window.opener.location.href='https://www.hao123. com' but it works, what is the reason
黄舟2017-06-26 10:54:27
<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();
可以关掉子窗口,在子窗口中无效。