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' 却可以,请问是什么原因
close 方法只能关闭由自己打开的window
<script type="text/javascript">
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("这是 'myWindow' ");
myWindow.document.write("<script>window.opener.close()</script>");
myWindow.focus();
myWindow.opener.document.write("这是父窗口");
myWindow.close();
雷雷 雷雷可以关闭掉子窗口,在子窗口中无效。