我如何处理ajax和关闭页面

时间:2021-05-30 00:08:09

If the user 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?

如果用户碰巧连接到我的网站有延迟,则ajax没有超时,用户决定关闭窗口。 ajax查询将被终止?有没有办法可以说仍然处理你关闭你想离开这个页面? (如果ajax成功将关闭,奖金)。我该怎么做呢?

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

我不确定这是否是同一件事(也许它内置到firefox?)但是当我关闭这个页面时它说

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?

我很肯定我看到了其他地方。如果用户没有提交ajax并且在帖子中间,我该如何显示?

2 个解决方案

#1


4  

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.

您可以使用window.onbeforeunload事件来处理此问题。在ajax请求开始时将变量设置为false。并且在回调函数中将其值设置为true,并在window.onbeforeunload事件中检查该变量并显示合适的消息。

Note

注意

This will fire when you refresh your page also.

这也会在您刷新页面时触发。

#2


1  

You can implement the onbeforeunload handler in js:

您可以在js中实现onbeforeunload处理程序:

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

#1


4  

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.

您可以使用window.onbeforeunload事件来处理此问题。在ajax请求开始时将变量设置为false。并且在回调函数中将其值设置为true,并在window.onbeforeunload事件中检查该变量并显示合适的消息。

Note

注意

This will fire when you refresh your page also.

这也会在您刷新页面时触发。

#2


1  

You can implement the onbeforeunload handler in js:

您可以在js中实现onbeforeunload处理程序:

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