开发者

How do i handle ajax and closing pages

If the u开发者_开发问答ser happens to have a delay in connecting to my site, the ajax hasnt timed out and the user decides to close the window. The ajax query will be terminated? Is there a way i can say still processing are you close you want to leave this page? (bonus if it will close once ajax was successful). How do i do this?

I am not sure if this is the same thing (maybe its built into firefox?) but when i closed this page it said

Are you sure you want to navigate away from this page?

You have started writing or editing a post.

Press OK to continue, or Cancel to stay on the current page.

I am positive i seen this other places. How do i make this appear when the user isnt submitting ajax and is in the middle of a post?


You can use the window.onbeforeunload event to handle this. Set a variable to false at the start of the ajax request. And in the callback function set its value to true, and in the window.onbeforeunload event check that variable and display suitable message.

Note

This will fire when you refresh your page also.


You can implement the onbeforeunload handler in js:

window.onbeforeunload = function() 
{
   if (showMessage)
   {
      return trye
   }
   else
   {
      return;
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜