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();
可以關掉子窗口,在子窗口中無效。
相關文章:
