onbeforeunload wait for ajax result

前端 未结 4 1440
梦谈多话
梦谈多话 2021-01-23 15:32

Is it possible for onbeforeunload() function to wait for ajax result and move to a jsp based on the resultvalue or atleast show a message before unloading?

4条回答
  •  野的像风
    2021-01-23 16:07

    It is not good Idea to display alert box for waiting response from ajax. Generally you can use alert box for message for confirmation of Leaving page after ajax response.

    For avoiding use of alert you can call Ajax synchronously for example

    if(window.XMLHttpRequest) xmlHttpObj=new XMLHttpRequest();
    else xmlHttpObj=new ActiveXObject("Microsoft.XMLHTTP");
    if(xmlHttpObj){
    xmlHttpObj.onreadystatechange=function(){
    if(xmlHttpObj.readyState==4 && xmlHttpObj.status == 200){
     // your logic required
    }
    }
    xmlHttpObj.open("GET", "your request url", false); // used false for synchronous call 
    xmlHttpObj.send(null);
    }
    else{
    alert("AJAX not support");
    }    
    

提交回复
热议问题