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
The close method can only close the window opened by itself
<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>
可以关掉子窗口,在子窗口中无效。