如何收集onbeforeunload的返回值

发布于 2024-11-18 14:51:03 字数 1153 浏览 3 评论 0原文

如果用户尝试关闭窗口而不保存表单,我将显示一条警告消息。

window.onbeforeunload = askConfirm;

    function askConfirm()
    {
        // needToConfirm is set to true if any changes are there in the form
        if (needToConfirm)
        {
            return "Your unsaved data will be lost.";
        }       
    } 

    function call_this_if_user_clicks_on_cancel()
    {
       // Bla bla bla
       // After this user should remain on the same page.
    }  

现在我想在用户单击“确定”时调用另一个函数。其余功能应保持不变。 那么如何收集 onbeforeunload 返回值并调用另一个函数?

解决方案:

    needToConfirm = false; 
    window.onbeforeunload = askConfirm;
    window.onunload = unloadPage;
    isDelete = true;

    function unloadPage()
    {
        if(isDelete)
        {
           call_this_if_user_clicks_on_cancel() 
        }   
    }

    function askConfirm()
    {
        alert("onbeforeunload");
        if (needToConfirm)
        {
            // Message to be displayed in Warning.
            return "Your data will be lost.";
        } 
        else
        {
            isDelete = false;
        }   
    }

I am displaying a warning message if user try to close window without saving the form.

window.onbeforeunload = askConfirm;

    function askConfirm()
    {
        // needToConfirm is set to true if any changes are there in the form
        if (needToConfirm)
        {
            return "Your unsaved data will be lost.";
        }       
    } 

    function call_this_if_user_clicks_on_cancel()
    {
       // Bla bla bla
       // After this user should remain on the same page.
    }  

Now I want to call another function when user clicks on Okay. And rest of the functionality should remain same.
So how can I collect onbeforeunload return value and call another function ?

SOLUTION:

    needToConfirm = false; 
    window.onbeforeunload = askConfirm;
    window.onunload = unloadPage;
    isDelete = true;

    function unloadPage()
    {
        if(isDelete)
        {
           call_this_if_user_clicks_on_cancel() 
        }   
    }

    function askConfirm()
    {
        alert("onbeforeunload");
        if (needToConfirm)
        {
            // Message to be displayed in Warning.
            return "Your data will be lost.";
        } 
        else
        {
            isDelete = false;
        }   
    }

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

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

发布评论

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

评论(1

四叶草在未来唯美盛开 2024-11-25 14:51:03

我不确定;但我认为你不能调用另一个函数,作为浏览器的预防措施,这样它就不会因离开页面而被困。

我认为只有浏览器知道它是否返回 true / false 来确定页面是否已卸载(或未卸载)。

以前unbeforeunload是非标准的(从IE4开始,但其他浏览器支持)
以下是 Mozilla 文档 https://developer.mozilla.org/en/DOM/window。在卸载之前
和 Microsoft 文档: http://msdn.microsoft.com/ en-us/library/ms536907(VS.85).aspx

不过,BeforeUnloadEvent 看起来像是在 HTML5 提案中。
本文档详细介绍了卸载文档时发生的步骤(读起来很有趣):

http://www.w3.org/TR/html5/history.html#beforeunloadevent


作为解决方法:
您可以从“unload 事件”中收集所需的信息,如果在 onbeforeunload 之后未调用 unload 事件,您可以假设用户选择留在您的页面上。

https://developer.mozilla.org/en/DOM/window.onunload

I'm not certain; But I would of thought you cannot call another function, as a preventative measure by the browser so it doesn't get trapped from leaving the page.

I think only the browser knows if it's returns true / false to determine if the page is unloaded (or not).

Previously unbeforeunload was non-standard (from IE4, but supported by other browsers)
Here are the Mozilla docs https://developer.mozilla.org/en/DOM/window.onbeforeunload
And Microsoft docs: http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx

However it looks as thought BeforeUnloadEvent is in the HTML5 proposal.
This document gives in detail the steps that happen when a document is unloaded (interesting read):

http://www.w3.org/TR/html5/history.html#beforeunloadevent


As a workaround:
You may be able to gather the information you need from the "unload event", if the unload event is NOT called after the onbeforeunload, you could assume that the user chose to stay on your page.

https://developer.mozilla.org/en/DOM/window.onunload

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