2012-02-25 78 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'); 
      }