为什么 OnBeforeUnload 使用 asp:LinkBut​​ton 触发两次?

发布于 2024-08-21 04:40:44 字数 842 浏览 7 评论 0原文

我有以下 Jquery 函数,当用户有未保存的更改(类似于 SO 的做法)并尝试离开当前屏幕时,会提示用户通知。

<!-- in my aspx page -->
<script type="text/javascript" language="javascript">
    window.onbeforeunload = function()
    {
        if (HasPendingUpdates())
        {
            return "Changes you have made will not be saved.";
        }
    }
</script>

这按预期工作,并在用户尝试单击 href 浏览或使用后退按钮等时显示消息框。我遇到的问题是我的链接之一是 asp:LinkBut​​ton因为它需要在离开页面之前触发一些服务器端代码。当用户单击此 LinkBut​​ton 时,他们会收到两次提示。

场景:

  1. 用户单击 LinkBut​​ton
  2. 出现提示,用户单击取消按钮。
  3. 提示消失并且用户仍在屏幕上。好。

  4. 用户单击LinkBut​​ton

  5. 出现提示并单击“确定”按钮。
  6. 提示消失并再次出现相同的提示。
  7. 用户再次单击“确定”。
  8. 提示消失,用户移动到下一个屏幕,一切都很好。

那么为什么我会收到第二个提示??? OnBeforeUnload 如何触发两次?

I have the following Jquery function to notify the user with a prompt when they have unsaved changes (similar to how SO does it) and are trying to leave the current screen.

<!-- in my aspx page -->
<script type="text/javascript" language="javascript">
    window.onbeforeunload = function()
    {
        if (HasPendingUpdates())
        {
            return "Changes you have made will not be saved.";
        }
    }
</script>

This works as expected and shows the message box when the user tries to click a href to browse away or use the back button etc. The problem I am having is that one of my links is a asp:LinkButton because it needs to fire off some server side code before leaving the page. When the user clicks this LinkButton they get the prompt twice.

Scenarios:

  1. User clicks the LinkButton
  2. Prompt appears and user clicks the cancel button.
  3. Prompt disappears and user is still on screen. GOOD.

  4. User clicks the LinkButton

  5. Prompt appears and clicks the OK button.
  6. Prompt disappears and the same prompt shows again.
  7. User clicks OK again.
  8. Prompt disappears and user moves to the next screen and all is good.

So why am I getting the second prompt??? How is the OnBeforeUnload firing twice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不交电费瞎发啥光 2024-08-28 04:40:45

我遇到了类似的问题,但是,我根本不想为 LinkBut​​tons 触发 onbeforeunload,因此我对 .aspx 页面上的每个 LinkBut​​ton 使用了以下内容:

OnClientClick="eval(this.href);return false"

LinkBut​​tons 的功能并没有被削弱,因为它们所做的只是提供带有 CommandArgument 的命令,供代码隐藏处理...

I had a similar problem, however, I didn't want to fire onbeforeunload for LinkButtons at all, therefore I used the following for every LinkButton on the .aspx page:

OnClientClick="eval(this.href);return false"

Functionality of the LinkButtons wasn't crippled, since all they did was supplying a Command with CommandArgument for the codebehind to handle...

眼藏柔 2024-08-28 04:40:45

使用 jQuery 添加处理程序会将它们堆叠起来。如果您为 href 分配带有某些 jQuery 函数的处理程序,然后再次为链接按钮分配它,则会有两个处理程序。

检查绑定 onunload 处理程序的频率,并确保取消绑定以前的处理程序或仅执行一次。

Adding handlers with jQuery stacks them. If you're assigning a handler with some jQuery function for your hrefs and then assigning it again for your linkbutton, there will be two handlers in place.

Check how often you are binding the onunload handler, and make sure that you're either unbinding previous handlers or only doing it once.

鼻尖触碰 2024-08-28 04:40:45

以下是解决此问题的方法:

    var checkChangesEnabled = true;
    window.onbeforeunload = confirmExit;

    function confirmExit() {
        if (checkChangesEnabled) {
            event.returnValue = "Changes you have made will not be saved.";
            disableCheck();
        }
    }

    function disableCheck() {
        checkChangesEnabled = false;
        setTimeout("enableCheck()", "100");
    }

    function enableCheck() {
        checkChangesEnabled = true;
    } 

Here is how to fix this:

    var checkChangesEnabled = true;
    window.onbeforeunload = confirmExit;

    function confirmExit() {
        if (checkChangesEnabled) {
            event.returnValue = "Changes you have made will not be saved.";
            disableCheck();
        }
    }

    function disableCheck() {
        checkChangesEnabled = false;
        setTimeout("enableCheck()", "100");
    }

    function enableCheck() {
        checkChangesEnabled = true;
    } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文