且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何prevent页面导航,直到AJAX调用完成

更新时间:2023-11-21 08:06:52

pretty的肯定,你可以在创建一个全球性的的.js 文件中像...

Pretty sure you can create a global in the .js file like...

VAR请求;

那么你的AJAX调用分配给该变量。

Then assign your ajax call to this variable.

request = $.ajax{
 //Ajax
 //Stuff
 //Goes
 //Here
}

现在你的 window.unbeforeunload 函数中,添加此条件语句。

Now inside your window.unbeforeunload function, add this conditional statement.

window.onbeforeunload = function(){
  if(!request){
    return "Request not initiated";
  }else{
    //Request is in progress...
    //You can use request.abort() if you need
  }
}

编辑:要在一些你可以在要求对象的看看这个页面。 (例如, .done 永远可能适合您的情况)

To elaborate on on some of the methods you can use on the request object, check out this page. (for example, .done or .always may suit your circumstances)