取消卸载窗口javascript没有确认

时间:2014-02-25 15:29:05

标签: javascript asp.net internet-explorer

我希望阻止客户端在我的asp.net应用程序中离开页面 如果没有保存,下面的代码可以选择离开页面而不保存,单击“确定” 在确认。 覆盖确认功能在我的浏览器中不起作用(ie8)

<script type="text/javascript">
    IsSaved = false;
    window.onbeforeunload = function (e) {
                    return IsSaved ;            } 
</script>

客户端保存数据:

IsSaved  = true;

编辑:  我想禁用点击“确定”按钮。 感谢。

2 个答案:

答案 0 :(得分:4)

您无法覆盖确认功能,因为它是一个确认功能。如果浏览器不会因为页面不想关闭而让用户关闭页面,那将是一个可怕的安全漏洞。

像其他人一样确认。并自动保存。

window.onbeforeunload = function() {
    return !isSaved && "You have unsaved (stuff). Are you sure you want to quit?";
};

答案 1 :(得分:0)

我创建了一个可以轻松包含在我所有网站中的库,以便轻松访问此功能。

将此类放在新的C#类库项目中,以便您可以从其他项目(网站)中引用它。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyNameSpace.ConfirmLeavePageScript
    {
    [DefaultProperty("TextToDisplay")]
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
    public class ConfirmLeavePageScript : WebControl
        {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("Are you sure you want to leave this page?")]
        [Localizable(true)]
        public string TextToDisplay
            {
            get
                {
                String s = (String)ViewState["TextToDisplay"];
                return ((s == null) ? "Are you sure you want to leave this page?" : s);
                }

            set
                {
                ViewState["TextToDisplay"] = value;
                }
            }

        protected override void RenderContents(HtmlTextWriter output)
            {
            string script = @"

            <script type='text/javascript'>
    var confirmOnPageExit = function (e)
    {
        // If we haven't been passed the event get the window.event
        e = e || window.event;

        var message = '" + TextToDisplay + @"';
        if(typeof ConfirmLeavePageMessage != 'undefined') //give client side the opportunity to overwrite the message instead of using message from ViewState.
            {
            message=ConfirmLeavePageMessage;
            }

        // For IE6-8 and Firefox prior to version 4
        if (e)
        {
            e.returnValue = message;
        }

        // For Chrome, Safari, IE8+ and Opera 12+
        return message;
    };

    function EnableConfirmOnPageExit()
        {
        // Turn it on - assign the function that returns the string
        window.onbeforeunload = confirmOnPageExit;
        }


function DisableConfirmOnPageExit()
        {
    // Turn it off - remove the function entirely
    window.onbeforeunload = null;
    }
</script>

            ";
            output.Write(script);
            }
        }
    }

您可以在网站项目中注册标记前缀,方法是将其添加到web.config中。确保您的网站添加C#类库项目作为参考(将它们放在相同的解决方案中)。

<configuration>
    <system.web>
        <pages>
            <controls>
                <add tagPrefix="tna" assembly="ConfirmLeavePage" namespace="MyNameSpace.ConfirmLeavePageScript" />
            </controls>
        </pages>
    </system.web>
</configuration>

然后,将控件放在内容页面的头部。

<head>
<tna:ConfirmLeavePageScript runat="server" TextToDisplay="Are you sure you want to leave without saving?" />
</head>

在检测是否已进行更改的JavaScript函数中,请调用此函数:

EnableConfirmOnPageExit();

您还可以执行以下操作(我认为这是不言自明的):

<script type="text/javascript>
ConfirmLeavePageMessage="new message"; //set a custom message from JavaScript for the confirmation window
DisableConfirmOnPageExit(); //Disables the confirmation window (user can leave page without seeing the confirmation window)
</script>