javascript - window.opener.close()無效
問題描述
1.父頁面window.open()打開新頁面
var targetWeb=null;if(targetWeb){ targetWeb.focus();}else{ targetWeb=window.open(’https://segmentfault.com’,’segmentfault’);}
2.子頁面中關(guān)閉父頁面
window.opener.close();
發(fā)現(xiàn)子頁面無法關(guān)閉父頁面,會提示:Scripts may close only the windows that were opened by it但若換成:window.opener.location.href=’https://www.hao123.com’ 卻可以,請問是什么原因
問題解答
回答1:close 方法只能關(guān)閉由自己打開的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();
可以關(guān)掉子窗口,在子窗口中無效。
相關(guān)文章:
1. 淺談vue生命周期共有幾個階段?分別是什么?2. Java EE 6:JSF與Servlet + JSP我應(yīng)該學(xué)習(xí)JSF嗎?3. java - hibernate正向工程生成一對多關(guān)系模型無報錯,但是只生成了一張表4. java - Spring使用@Autowired失效但是getBean()可以執(zhí)行成功5. javascript - js正則匹配小括號中的內(nèi)容6. macos - mac下docker如何設(shè)置代理7. index.php錯誤,求指點8. 微信開放平臺 - android 微信支付后點完成按鈕,后回調(diào)打開第三方頁面,屏幕閃動,求解決方法9. 微信公眾號在線生成二維碼帶參數(shù)怎么搞?10. css box-shadow 單邊 單角陰影
