如何收集onbeforeunload的返回值 [英] How to collect return value of onbeforeunload

查看:317
本文介绍了如何收集onbeforeunload的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.
    }  

现在我想在用户点击好。其余功能应该保持不变。
那么如何收集 onbeforeunload 返回值并调用另一个函数?

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 ?

解决方案:

    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;
        }   
    }

谢谢Alex。

推荐答案

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

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.

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

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

以前unbeforeunload是非标准的(来自IE4,但受其他浏览器支持)
以下是Mozilla文档 https://developer.mozilla.org/en/ DOM / window.onbeforeunload
和Microsoft docs: http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx

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

然而它看起来像以为BeforeUnloadEvent在HTML5提案中。
本文档详细介绍了卸载文档时发生的步骤(有趣的阅读):

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

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

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

这篇关于如何收集onbeforeunload的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆