一、問題
在Chrome
瀏覽器中調(diào)用window.close()
關(guān)閉當(dāng)前頁面時(shí)瀏覽器控制臺(tái)報(bào)出 Scripts may close only the windows that were opened by them.
且無法關(guān)閉當(dāng)前頁面。
先上結(jié)論:不是通過JS
打開的瀏覽器標(biāo)簽,都不能通過window.close()
來關(guān)閉當(dāng)前頁面
二、原因
MDN
對(duì)于window.close()
的解釋如下所示:
文章里說,close
只能關(guān)閉用在JS
腳本里用open
方法打開的窗口。因此下面的代碼有效。
var win = window.open("https://www.oschina.net");
win.close();
或者說在窗口A
執(zhí)行代碼
window.open("http://example.com")
然后在新創(chuàng)建的example.com
窗口執(zhí)行下面的代碼也可以關(guān)閉。
window.close()
三、解決
在當(dāng)前頁面自身通過
window.open()
方法打開一個(gè)空白頁面
調(diào)用window.close()
關(guān)閉文章來源:http://www.zghlxwxcb.cn/news/detail-806804.html
window.open("about:blank", "_self")
window.close();
四、最后
本人每篇文章都是一字一句碼出來,希望對(duì)大家有所幫助,多提提意見。順手來個(gè)三連擊,點(diǎn)贊??收藏??關(guān)注?,一起加油?文章來源地址http://www.zghlxwxcb.cn/news/detail-806804.html
到了這里,關(guān)于js關(guān)閉當(dāng)前窗口報(bào)錯(cuò)Scripts may close only the windows that were opened by them的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!