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. 在應用配置文件 app.php 中找不到’route_check_cache’配置項2. html按鍵開關如何提交我想需要的值到數據庫3. mysql取模分表與分表4. HTML 5輸入框只能輸入漢字、字母、數字、標點符號?正則如何寫?5. gvim - 誰有vim里CSS的Indent文件, 能縮進@media里面的6. dockerfile - 我用docker build的時候出現下邊問題 麻煩幫我看一下7. 跟著課件一模一樣的操作使用tp6,出現了錯誤8. PHP類屬性聲明?9. objective-c - ios 怎么實現微信聯系列表 最好是swift10. javascript - 請教如何獲取百度貼吧新增的兩個加密參數
