關閉當前瀏覽器選項卡javascript程式碼
Firefox火狐瀏覽器預設情況下,windows.close()對於非window.open的頁面無效,需要windows.close()生效需要修改瀏覽器的配置,即在firefox瀏覽器的位址列輸入:about:config然後找到dom.allow_scripts_to_close_windows;把false改為true,但是這對於網站來說,不可能去修改使用者的瀏覽器設定的,因此還是要通過前端程式碼解決。
既然Firefox瀏覽器不支援window.close(),但是可以通過開啟新空白選項卡about:blank的方式實現關閉當前選項卡,雖然不是真正意義上的關閉當前視窗,但也算是偽關閉吧。
javascript程式碼:
function pageClose(){ if (navigator.userAgent.indexOf("MSIE") > 0) { if (navigator.userAgent.indexOf("MSIE 6.0") > 0) { window.opener = null; window.close(); } else { window.open('', '_top'); window.top.close(); } } else if (navigator.userAgent.indexOf("Firefox") > 0) { window.location.href = 'about:blank '; } else { window.opener = null; window.open('', '_self', ''); window.close(); } }
關閉按鈕html程式碼:
<a class="close" href="javascript:void(0);" onclick="pageClose();">關閉</a>
附:window.close();對於各瀏覽器視窗關閉的支援情況
序號 | 關閉程式碼 | 需要確認 | 無任何作用 | 無需確認 |
---|---|---|---|---|
1 | window.close() | IE7 |
firefox,chrome, safari |
Opera |
2 |
window.opener=null; window.open('','_self'); window.close(); |
firefox |
IE7,Opera, chrome,safari |
|
3 |
window.open('','_self'); window.close(); |
firefox |
IE7,Opera, chrome,safari |
|
4 |
window.opener=null; window.close(); |
IE7 | firefox,safari | chrome,Opera |
5 |
var opened=window.open('about:blank','_self'); opened.opener=null; opened.close(); |
firefox |
safari,IE7, chrome,Opera |
|
6 |
var opened=window.open('about:blank','_self'); opened.close(); |
safari,firefox |
firefox,IE7, chrome,Opera |