首页 > 解决方案 > Web 应用程序关闭浏览器消息

问题描述

我使用 ASP.net 构建了一个简单的 Web 应用程序,在该应用程序中我关闭了一个 Web 浏览器选项卡,它会通过一个弹出框向用户发出警告消息,提示他即将关闭浏览器。

我用来添加弹出框功能的以下 javascript 代码:

window.onbeforeunload = function () {
   return "Are you sure";
};

这适用于 Internet Explorer,但不适用于 chrome。我还没有检查火狐。

我知道 chrome 你不能有自定义消息,但我根本无法让弹出框工作。

在创建适用于所有主要浏览器(如 Internet Explorer、chrome 和 Firefox)的弹出框警告方面是否有其他选择?

另外,我已经测试了这个 chrome 最新版本和 66 版本。

编辑

这不是Chrome 中 window.onbeforeunload 的副本:最近的修复是什么?

那里的用户可以在 chrome 上显示一个简单的弹出框。我无法显示任何东西。但是,当我在开发人员工具窗口(F12)中并调试代码时,我注意到一些奇怪的事情,我得到了弹出框。

添加代码

我使用了来自 ASP.NET Web Forms 的默认模板,它创建了一个简单的应用程序。

我添加了名为 CheckBrowserClose.js 的 javascript,其中包含一行:

window.onbeforeunload = function () { return 'Are you sure?' };

我已将此脚本添加到脚本包中。

 public class BundleConfig
    {
        // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkID=303951
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/WebFormsJs").Include(
                            "~/Scripts/WebForms/WebForms.js",
                            "~/Scripts/WebForms/WebUIValidation.js",
                            "~/Scripts/WebForms/MenuStandards.js",
                            "~/Scripts/WebForms/Focus.js",
                            "~/Scripts/WebForms/GridView.js",
                            "~/Scripts/WebForms/DetailsView.js",
                            "~/Scripts/WebForms/TreeView.js",
                            "~/Scripts/WebForms/WebParts.js",
                            "~/Scripts/WebForms/CheckBrowserClose.js"));

            // Order is very important for these files to work, they have explicit dependencies
            bundles.Add(new ScriptBundle("~/bundles/MsAjaxJs").Include(
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjax.js",
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjaxApplicationServices.js",
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjaxTimer.js",
                    "~/Scripts/WebForms/MsAjax/MicrosoftAjaxWebForms.js"));

            // Use the Development version of Modernizr to develop with and learn from. Then, when you’re
            // ready for production, use the build tool at http://modernizr.com to pick only the tests you need
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                            "~/Scripts/modernizr-*"));

            ScriptManager.ScriptResourceMapping.AddDefinition(
                "respond",
                new ScriptResourceDefinition
                {
                    Path = "~/Scripts/respond.min.js",
                    DebugPath = "~/Scripts/respond.js",
                });
        }
    }

标签: javascript

解决方案


推荐阅读