2012-02-25 53 views
1

所以我一直在找幾個小時,測試多個版本,測試一些我自己的理論,我似乎無法得到它的工作。jQuery退出彈出重定向?

我想要做的是使用alertconfirm(或任何作品),所以當用戶嘗試從購買表單導航時彈出一個對話框。我只想問他們:「嘿,不要離開,爲什麼不能免費諮詢?」並將用戶重定向到「免費諮詢」表格。

這是我到目前爲止,我只是沒有得到正確的結果。

$(window).bind('beforeunload', function(){ 
     var pop = confirm('Are you sure you want to leave? Why not get a FREE consultation?'); 
     if (pop) { 

      window.location.href('http://www.mydomain/free-consultation/'); 
     } else { 
      // bye bye 
     } 
    }); 

    $("form").submit(function() { 
     $(window).unbind("beforeunload"); 
    }); 
+0

注意,Firefox會忽略你設置的任何消息,並顯示了自己:https://bugzilla.mozilla.org/show_bug.cgi?id=588292 – Interrobang 2012-02-25 07:15:44

回答

1

而不是使用beforeunloadalert(),我決定檢查用戶鼠標是否已離開文檔。請參見下面的代碼:

$(document).bind('mouseleave', function(event) { 
    // show an unobtrusive modal 
}); 
0

採取

function setDirtyFlag() { 
    needToConfirm = true; //Call this function if some changes is made to the web page and requires an alert 
    // Of-course you could call this is Keypress event of a text box or so... 
} 

function releaseDirtyFlag() { 
    needToConfirm = false; //Call this function if dosent requires an alert. 
    //this could be called when save button is clicked 
} 


window.onbeforeunload = confirmExit; 
function confirmExit() { 
    if (needToConfirm) 
     return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?"; 
} 

腳本試試這個:

window.onunload = redirurl; 
      function redirurl() { 
       alert('Check this Page'); 
       window.location.href('http://www.google.com'); 
      }