【问题标题】:Cancel unload window javascript without confirm取消卸载窗口javascript而不确认
【发布时间】:2014-02-25 15:29:05
【问题描述】:

我想阻止客户端在我的 asp.net 应用程序中离开页面 不保存,以下代码获取选项,通过单击“确定”离开页面而不保存 在确认中。 覆盖确认功能在我的浏览器中不起作用(ie8)

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

客户端保存数据的位置:

IsSaved  = true;

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

【问题讨论】:

  • 我不确定你的问题是什么......
  • 我也避免在确认中点击“确定”,禁止不保存就离开页面

标签: javascript asp.net internet-explorer


【解决方案1】:

您不能覆盖确认函数,因为它是一个确认函数。如果浏览器仅仅因为页面不想被关闭而不允许用户关闭页面,那将是一个可怕的安全漏洞。

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

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

【讨论】:

  • 我也避免在确认中点击“确定”,禁止不保存就离开页面
  • @mich 就像他说的,那是不可能的。
【解决方案2】:

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

将这个类放在一个新的 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>

【讨论】:

  • 不确定您所说的“所有代码”是什么意思。 JavaScript 功能支持新旧浏览器。它还可以轻松地从服务器端修改显示的消息,并使其易于在多个项目之间移植。
猜你喜欢
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 2013-12-30
  • 2021-11-11
  • 2012-09-10
相关资源
最近更新 更多