2

I am using jQuery bind:

$(window).bind('beforeunload', function() {
   //code
   return "";
   }

If I do not give return "", it works fine in mozilla firefox, IE8. It does not give an alert box saying "Are you sure you want to navigate away from this page?" However in google chrome, beforeunload event does not work without return "" statement.

And if I use return"", it gives an alert box in all broswers.

I do not want the dialog box and I want beforeunload event to work. Please help. Please suggest if there is any other alternative solution to this. Thanks.

4

1 回答 1

1

onbeforeunload 在浏览器中的行为不一致

您应该将所有 ajax 调用设置为async false在 beforeunload 事件中调用的内部函数,然后尝试这个丑陋的 hack:

$(window).on('beforeunload', function (e) {
    if (e.originalEvent) 
       //Call function for updating omniture site 
    else  //this is to provide enough time to all your other requests to be send
    $.get("", {
        async: false
    });
    $(this).trigger('beforeunload'); //will create kind of infinite loop
    //this is an ugly hack because many useless request will be send (but nowhere)
}

测试它并告诉我。

于 2013-07-31T12:21:53.703 回答