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();
可以關掉子窗口,在子窗口中無效。
相關文章:
1. 小程序怎么加外鏈,語句怎么寫!求救新手,開文檔沒發現2. javascript - 在 vue里面用import引入js文件,結果為undefined3. javascript - vue-resource中如何設置全局的timeout?4. html5和Flash對抗是什么情況?5. 前端 - node vue webpack項目文件結構6. Java反射問題:為什么android.os.Message的recycleUnchecked方法不能通過反射獲取到?7. php如何獲取訪問者路由器的mac地址8. 多選框寫進數據庫怎么寫9. 求教一個mysql建表分組索引問題10. thinkPHP5中獲取數據庫數據后默認選中下拉框的值,傳遞到后臺消失不見。有圖有代碼,希望有人幫忙
