简体   繁体   English

ASP.NET linkbutton两次提高onBeforeUnload事件

[英]ASP.NET linkbutton raising onBeforeUnload event twice

I've posted this here , but thought it might deserve a question on its own. 我在这里发布了这个,但认为它本身可能值得一个问题。

What I'm trying to do is show a dialog box that asks the user if he/she wants to leave the page if there are unsaved changes. 我正在尝试做的是显示一个对话框,询问用户是否有未保存的更改时他/她是否要离开页面。 That all works fine. 一切正常。 But the problem is described below: 但问题描述如下:

Has anyone come across the problem where Internet Explorer fires the onbeforeunload event twice? 有没有人遇到过Internet Explorer两次触发onb​​eforeunload事件的问题? While Googling around, I found it has something to do with the fact that for (among others) an ASP.NET linkbutton the HTML code is <a href="javascript: __doPostBack... . 在谷歌搜索时,我发现它与(以及其他)ASP.NET链接按钮的事实有关,HTML代码是<a href="javascript: __doPostBack...

Apparently, when IE encouters a link that doesn't have a href="#" , it fires the onbeforeunload event. 显然,当IE鼓励一个没有href="#" ,它会触发onb​​eforeunload事件。 Then, when you confirm the javascript dialog box we're showing, the page will do the 'real' unload to navigate to the other page, and raise the onbeforeunload event a second time. 然后,当您确认我们正在显示的javascript对话框时,页面将执行“真实”卸载以导航到另一个页面,并再次引发onbeforeunload事件。

A solution offered on the internet is to set a boolean variable and check on it before showing the dialog. 互联网上提供的解决方案是设置一个布尔变量并在显示对话框之前检查它。 So the second time, it wouldn't be shown. 所以第二次,它不会显示。 That's all well, but when the user cancels, the variable will still be set. 这一切都很好,但是当用户取消时,仍然会设置变量。 So the next time the user wants to leave the page, the dialog won't be shown anymore. 因此,下次用户想要离开页面时,对话框将不再显示。

Hope this is a little clear, and I hope someone has found a way around this? 希望这有点清楚,我希望有人找到解决办法吗?

In reaction to annakata: Yes, but you want the result of the dialog box to be used by the browser. 对annakata的反应:是的,但您希望浏览器使用对话框的结果。 So you might think using 'return bFlag' would do the trick (or event.returnValue = bFlag), but that gives you a second dialog box. 所以你可能会认为使用'return bFlag'可以做到这一点(或者event.returnValue = bFlag),但这会给你第二个对话框。 I've found a way around, thanks to this page . 由于这个页面 ,我找到了一条路。 It's quite simple actually: 实际上很简单:

var onBeforeUnloadFired = false;

Use this global variable here: 在这里使用此全局变量:

if (!onBeforeUnloadFired) {
    onBeforeUnloadFired = true;
    event.returnValue = "You'll lose changes!";
}
window.setTimeout("ResetOnBeforeUnloadFired()", 1000);

And then implement that function: 然后实现该功能:

function ResetOnBeforeUnloadFired() {
    onBeforeUnloadFired = false;
}

So, in effect, use the flag, but reset it if the user clicks cancel. 因此,实际上,使用标志,但如果用户单击取消,则重置它。 Not entirely what I would like, but haven't found anything better. 不完全是我想要的,但没有找到更好的东西。

I haven't encountered this, but surely you could set the flag variable to be equal to the result of the dialog? 我没有遇到过这个,但你肯定可以将flag变量设置为等于对话框的结果吗? If the user cancels the flag will therefore remain false. 如果用户取消该标志将因此保持为假。

var bFlag = window.confirm('Do you want to leave this page?');

IE supports an event on the document object called onstop. IE支持名为onstop的文档对象上的事件。 This event fires after the onbeforeunload event, but before the onunload event. 此事件在onbeforeunload事件之后但在onunload事件之前触发。 This isn't exactly pertinent to your two dialogs question, but its still relevant to other people that might stumble on this thread ( as I did ). 这与你的两个对话框问题并不完全相关,但它仍然与可能偶然发现这个线程的其他人相关(正如我所做的那样)。

The problem I was having, was that I needed to display a loading message upon the onbeforeunload event firing. 我遇到的问题是,我需要在onbeforeunload事件触发时显示加载消息。 This is all fine until the page has some sort of confirm dialog on it. 这一切都很好,直到页面上有某种确认对话框。 The onbeforeunload event fires even if the user cancel's and remains on the page. 即使用户取消并且仍在页面上,onbeforeunload事件也会触发。 The easiest thing for me to do was to move my "loading" display logic into a handler for document.onstop. 对我来说最简单的事情就是将我的“加载”显示逻辑移动到document.onstop的处理程序中。 On top of this, you have to check the readyState property of the document object, which is another IE-only field. 除此之外,您还必须检查文档对象的readyState属性,该属性是另一个仅限IE的字段。 If its "loading", it means the user is actually leaving the page. 如果是“加载”,则表示用户实际上正在离开页面。 If its "complete", it means the user is staying. 如果它“完整”,则表示用户正在停留。

If you are fine with just using IE, then you might be interested in the following. 如果你只使用IE就可以了,那么你可能会对以下内容感兴趣。

document.onstop = function() 
{
    try
    {
        if( document.readyState != "complete" )
        {
            showLoadingDiv();
        }
    }
    catch( error )
    {
        handleError( error );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM