1. 程式人生 > >關閉頁面之前 提示使用者(確定跳轉,取消停留)----使用window.onbeforeunload

關閉頁面之前 提示使用者(確定跳轉,取消停留)----使用window.onbeforeunload

Detecting When The User Has Clicked Cancel 

One of the things you may want to do is to be notified when the user clicks cancel, aborting a page unload. Unfortunately there's no way to be immediately notified. The best you can do is to set a unique global variable in your "onbeforeunload" event and then look to see if that variable has been set in other functions. There is no way to get an immediate notification that the user has aborted a page unload. 


The example code I used above to do an example of an "onbeforeunload" dialog is as follows: 

Java程式碼  收藏程式碼
  1. var _isset=0;  
  2. function demo() {  
  3.    window.onbeforeunload = function () {  
  4.       if (_isset==0) {  
  5.          _isset=1;  // This will only be seen elsewhere if the user cancels.  
  6.          return
     "This is a demonstration, you won't leave the page whichever option you select.";  
  7.       }  
  8.    }  
  9.    _isset=0;  
  10.    window.location.reload();  
  11.    return false;  
  12. }  

This code defines a global variabled named _isset, and then initializes it to zero. In our "onbeforeunload" event the variable is checked and if it's set to one, no unload dialog box will appear. The only way _isset could ever be one is if the user previously aborted a page unload. 


But as you can see this method won't help you if you need to be immediately notified that that the user has finished dealing with the confirmation box. You can detect when it appears on the screen but there's no way to know when the user has finished interacting with it if the user clicked cancel (if the user clicked OK, then of course the unload event will have been tripped). 
-------------------------------------------------------------- 
雖然如此,但還是有高手給出瞭如下程式碼 ^^ 

Java程式碼  收藏程式碼
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"  
  2.    "http://www.w3.org/TR/html4/strict.dtd">  
  3. <html><head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
  5. <meta http-equiv="Content-Style-Type" content="text/css">  
  6. <meta http-equiv="Content-Script-Type" content="text/javascript">  
  7. <title>onbeforeunload test</title>  
  8. <script type="text/javascript"><!--  
  9. window.onbeforeunload = function() {  
  10.   // in Firefox and Netscape 7.2+ the setTimeout or setInterval do not wait   
  11.   // to be executed until after the user clicks one of the buttons in the   
  12.   // confirm()-like box.  
  13.   //setTimeout("alert('hi from setTimeout()');",500);  
  14.   // setTimeout() and setInterval() aren't called when ok is clicked in   
  15.   // IE5-6/Win, but is called in IE7 when the time is short, but not when   
  16.   // it's longer, like 500 (a half second).  
  17.   window.unloadTimer = setInterval(  
  18.     "alert('hi from setInterval()');clearInterval(window.unloadTimer);",500);  
  19.   window.onunload = function() {clearInterval(window.unloadTimer);}  
  20.   return 'onbeforeunload testing';  
  21. }  
  22. // -->  
  23. </script>  
  24. </head>  
  25. <body>  
  26. <h1>onbeforeunload test</h1>  
  27. </body>  
  28. </html>  

Java程式碼  收藏程式碼
  1. <script type="text/javascript">  
  2. //<![CDATA[  
  3. var  
  4.   is_asked = false;  
  5. window.onbeforeunload =   
  6.   function (ev) {  
  7.     var e = ev || window.event;  
  8.     window.focus();  
  9.     if (!is_asked){  
  10.       is_asked = true;  
  11.       var showstr = "CUSTOM_MESSAGE";  
  12.       if (e) {  //for ie and firefox  
  13.         e.returnValue = showstr;   
  14.       }  
  15.       return showstr; //for safari and chrome  
  16.     }  
  17.   };  
  18. window.onfocus =  
  19.   function (ev){  
  20.     if (is_asked){  
  21.       window.location.href = "http://www.google.com";  
  22.     }  
  23.   }  
  24. //]]>  
  25. </script  

相關推薦

關閉頁面之前 提示使用者確定取消停留----使用window.onbeforeunload

Detecting When The User Has Clicked Cancel One of the things you may want to do is to be notified when the user clicks cancel, aborting a

Markdown基礎內含:錨點使用使用HTML頁面目錄生成

Github樣式顯示參考:點我 之前說過用word寫文章,這次說說Markdown寫文章(推薦) 逆天推薦使用VSCode編寫 裝這個外掛寫作更方便: 內含:錨點使用,使用HTML,新頁面跳轉,目錄生成 啟用方式: H1~H3(#的個數)[部落格園只支援H1~3] # H1 ## H2 ### H3

阻止 android webview 中的連結跳出APP到系統瀏覽器

           //通過此設定,可以讓新頁面的跳轉不用跳出APP, 否則,新的頁面會跳轉到瀏覽器                   webView.setWebViewClient(new WebViewClient() {             @Override             publ

html 中a標籤的問題無反應呼叫方法

讓超連結點選後不跳轉,可以用href = "#",但是這個#就會錨點到頁面最上邊     點選連結後不跳轉可以設定成     1.<a href="javascript:void(0);" >javascript:void(0);</a>     2.

Android使用okhttp封裝多檔案批量下載 帶進度條取消下載

在網上搜索了很多關於okhttp封裝的網路框架,唯獨沒找到完美實現了多個檔案批量下載的案例,當前使用的最多的也就是okhttp了,所以,我學習了各位大神的封裝後,自己也試著封裝了一個關於okhttp的網路請求框架,方便專案中的使用。 實現的功能基本如下:

shiro登入驗證登入到指定頁面驗證碼驗證不登出之前已登入使用者下再次登入

web.xml配置 <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springfr

jQuery使用ajaxSubmit()提交表單在不希望頁面的情況下

ont .ajax 引入 當前 data fun get orm mit 原文:http://www.jb51.net/article/48728.htm ajaxSubmit(obj)方法是jQuery的一個插件jquery.form.js裏面的方法,所以使用此方法需要先

各種頁面定時倒計時代碼總結

倒數 自動跳轉 str ani import rect ext document color 一、使用setTimeout函數實現定時跳轉(如下代碼要寫在body區域內) <script type="text/javascript"> //3秒鐘之後跳

wamp下DVWA登入頁面無法訪問或輸入login直接到setup介面的解決辦法

老王上網找了好久解決辦法,有說改埠的、有說重啟動的balabala但都沒亂用....真的急瘋了,前兩天在機電樓數字簽名課上中期檢查還好好的,突然輸入login的網址就登不上去了。。。在我一籌莫展抓耳撓腮之時,靈光一現,發現了下面的解決辦法: 大家開啟本地服務(就是下方輸入框

servlet中實現提示登入成功後到某一頁面

一般J2ee中Jsp+Servlet+JavaBean的servlet中實現的是DoGet和DoPost函式,主要用到DoPost函式打印出某一頁面。 我在servlet中進行了身份驗證,驗證成功後,提示登入成功,之後跳轉到個人主頁等其他頁面,也可以實現提示

小宗的啟示:不同型別的liferay使用者登入到不同頁面登入個性化——liferay5.0.1使用者登入後的

在liferay5.0.1使用者登入後的跳轉(一) 中,提到新建使用者在去掉首次登入的條例說明,可以自動跳轉到portal.properties設定的固定頁面上。 問題又來了,在實際應用中,不同使用者需要登入到不同頁面進行。而liferay設定的登入跳轉是侷限的,使得不同使用

關閉頁面提示是否關閉

轉發: 轉發頁面 方法/步驟 jquery離開頁面彈出提示 1jquery離開頁面彈出提示程式碼://繫結beforeunload事件$(window).bind('beforeunload',function(){return '您輸入的內容尚未儲存,確定離開此頁面嗎?'

hao643.com劫持修改快捷方式至hao123.com

手工 mob 彈出 let shell program data 喜歡 lin >癥狀:所有瀏覽器快捷方式,都被加上尾巴,例如IE的:"C:\Program Files\Internet Explorer\iexplore.exe" http://hao643.com

hao643.com劫持IE和Chrome等被修改快捷方式到hao123.com

cnblogs path eat ams app .get style 選型 des 最近下載了某個軟件,安裝後IE和Chrome的Startup Page均被重定向到hao123.com。查看IE和Chrome的配置選型,沒發現問題。後來發現是快捷方式後邊多了一串字符串

減少頁面加載時間感知或實際加載時間的方法。

dsm 完全 圖片合成 空格 數量 pos 允許 較高的 trac 版權聲明:本文為博主原創文章,未經博主允許不得轉載。 1. 盡量減少頁面中重復的HTTP請求數量 比較直接的理解就是要減少調用其他頁面、文件的數量。我們在使用css格式控制的時候,經常會采用b

頁面定時刷新或自動

eth blog www. 方法 mage ase logs res set 采用response對象的setHeader方法可實現頁面定時跳轉或定時自刷新。 1.response.setHeaser("refresh","5");//每隔5秒,頁面自刷新一次 2.resp

【前臺】整個項目實現單頁面拋棄iframe

點擊事件 true charset req 做到 value images tro Language 即如下: 【想做到點擊nav側邊欄,僅替換右邊div中的內容,而不是跳轉到新的頁面,這樣的話,其實整個項目中就只有一個完整的頁面,其他的頁面均只寫<body>內

利用cookie實現刷新頁面左側菜單點擊後狀態保持不變。

coo spl 失效 不變 find sco a標簽 左側菜單 cookie 是一個後臺關系系統,後臺是phyton,後臺把左側菜單都是利用a標簽進行跳轉,導致菜單點擊狀態在頁面刷新後失效,用戶體驗不好,被要求改掉。 查了一些資料。利用cookie終於搞定了這個大問題。主要

nginx填坑補充nginx根據上下文ip或者域名

store access allow tro 跳轉 head 不同的 域名 root 今天有一個需求,要根據上下文調到不同的ip或域名地址,使用上下文做域名跳轉的時候,proxy_pass域名後面一定要帶‘/’否則會把nginx的上下文自動帶入,這樣就行。 location

頁面A到BB再返回A時自動定位到離開A時的位置

ID 重新 before setview one attr UNC -- ntb <template> <div class="hello" @scroll="scrollLoad" id="myScrollBox"> <h