JS針對瀏覽器視窗關閉事件的監聽方法集錦,js瀏覽器視窗
本文執行個體總結了JS針對瀏覽器視窗關閉事件的監聽方法。分享給大家供大家參考,具體如下:
方式一:(適用於IE瀏覽器,而且重新整理不提示,只在點擊瀏覽器關閉按鈕的時候提示)
<script type="text/javascript">window.onbeforeunload=onclose;function onclose(){if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey){return "您要離開嗎?";}}</script>
方式二:適用於IE和FF,不區分重新整理和關閉
<script type="text/javascript"> window.onbeforeunload = onbeforeunload_handler; window.onunload = onunload_handler; function onbeforeunload_handler(){ var warning="確認退出?"; return warning; } function onunload_handler(){ var warning="謝謝光臨"; alert(warning); }</script>
方式三:適用於IE和FF,不區分重新整理和關閉,最簡單的
<script type="text/javascript">window.onbeforeunload=onclose;function onclose(){return "您確定退出嗎?";}</script>
方式四:適用於IE和FF,不區分重新整理和關閉,稍複雜的
<script language="javascript">var MSG_UNLOAD="如果你此時離開檔案系統,所做操作資訊將全部丟失,是否離開?";var UnloadConfirm = {};//啟用監聽瀏覽器重新整理、關閉的方法UnloadConfirm.set = function(confirm_msg){ window.onbeforeunload = function(event){ event = event || window.event; event.returnValue = confirm_msg; }}//關閉監聽瀏覽器重新整理、關閉的方法UnloadConfirm.clear = function(){ window.onbeforeunload = function(){};}UnloadConfirm.set(MSG_UNLOAD);</script>
方式五:只適用於IE6下的關閉按鈕和快速鍵關閉的,重新整理不提示
<script type="text/javascript">window.onbeforeunload=onclose;function onclose(){var warnning = '<fmt:message key="systemMessage.exitWarning" />';var beforeExit='<fmt:message key="systemMessage.beforeExitWarning" />'; if(event.clientY<0 && event.clientX>document.body.clientWidth-20 || event.clientY<0 && event.clientX<20 ||event.altKey || event.ctrlKey || event.clientY>document.body.clientHeight){alert(beforeExit);return warnning;}}</script>
另附判斷瀏覽器類型的JS
<script type="text/javascript"> var Sys = {}; var ua = navigator.userAgent.toLowerCase(); if (window.ActiveXObject) Sys.ie = ua.match(/msie ([\d.]+)/)[1] else if (document.getBoxObjectFor) Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1] else if (window.MessageEvent && !document.getBoxObjectFor) Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1] else if (window.opera) Sys.opera = ua.match(/opera.([\d.]+)/)[1] else if (window.openDatabase) Sys.safari = ua.match(/version\/([\d.]+)/)[1]; //以下進行測試 if(Sys.ie) document.write('IE: '+Sys.ie); if(Sys.firefox) document.write('Firefox: '+Sys.firefox); if(Sys.chrome) document.write('Chrome: '+Sys.chrome); if(Sys.opera) document.write('Opera: '+Sys.opera); if(Sys.safari) document.write('Safari: '+Sys.safari);</script>
區分瀏覽器,IE和FF分別處理(奇怪的是,IE下有時候失效)
<script type="text/javascript">window.onbeforeunload=onclose;function onclose(){var Sys = {};var warnning = '<fmt:message key="systemMessage.exitWarning" />';var ua = navigator.userAgent.toLowerCase();if (window.ActiveXObject) Sys.ie = ua.match(/msie ([\d.]+)/)[1]else if (document.getBoxObjectFor) Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1]if(Sys.ie) {//for IEif(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey){window.event.returnValue = warnning ;}}if(Sys.firefox) //for FFreturn warnning;}</script>
最簡單的判斷瀏覽器類型的方法
<script type="text/javascript">if(-[1,]){ alert("這不是IE瀏覽器!");}else{ alert("這是IE瀏覽器!");}</script>
[1,]在標準瀏覽器會返回字串"1",相當於調用[1,].toString,
,IE則返回"1,"。但是這樣IE與標準都會通過檢測,因此使用負號強制轉換為數字,
標準能成功轉換為1,1會在if中自動轉換為true,而IE則轉換為NaN,再自動轉換為false!