在回发之前停止用户离开 [英] Stop User Leaving Before Postback
问题描述
我在回发上运行了多个功能,这些功能可能需要一些时间才能完成.
启动回发后,我将显示带有以下代码的加载图像:
I have several functions running on a postback that can take a little time to complete.
When postback is initiated I show a loading image with this code:
function showLoader()
{
document.getElementById("<%=loadingImage.ClientID%>").style.visibility="visible";
}
我希望能够向此功能添加代码,因此,如果用户此时尝试离开,则会通知他们操作尚未完成.
我找到了以下代码:
I want to be able to add code to this function so if user tries to leave at this point they are informed the operation is not complete.
I found this code:
function goodbye(e) {
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
window.onbeforeunload=goodbye;
这可行,但是我只希望代码在加载图像处于活动状态时处于活动状态.
我尝试了以下操作,但是当页面最终发回时,它会显示警告消息:
This works but I only want the code to be active when the loading image is active.
I tried the following but it shows the alert message when the page eventualy posts back:
function goodbye(e) {
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
function showLoader()
{
document.getElementById("<%=loadingImage.ClientID%>").style.visibility="visible";
window.onbeforeunload=goodbye;
}
有什么想法可以调整,使其仅在用户离开页面时显示,而在回发完成时不显示?
Any ideas how I can tweak this to just show when user leaves page and not when postback completes?
推荐答案
您知道您遇到的问题比用户还大离开.您的操作必须独立于用户状态.为什么?您不能阻止用户关闭浏览器!
You know you''ve got a bigger problem than the user leaving. Your operation has to be independant of the users state. Why? You cannot prevent the user from closing the browser!
这篇关于在回发之前停止用户离开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!