javascript - 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",
});
}
}
解决方案
推荐阅读
- android - 如果我之前已经下载过,如何避免从 firebase 下载图像?
- neo4j - Neo4j 社区服务器和 Gephi 之间的连接失败
- python - H2O-3 Flow(UI) 和 XGBoost
- python - 在python中制作随机字符串的问题
- java - 单个链表中头尾插入的区别
- c++ - 该标准是否说明了异常和不同调用约定的共存?
- google-cloud-platform - 如何在 GCP 中下载整个存储桶?
- python - 如何有效地管理可以删除其中一个元素或与下一个元素交换的元素列表?
- c++ - 由点和字母 c++ 组成的图像
- node.js - 通过 Firebase 函数触发器在没有 req/res 对象的情况下调用 cors(req,res,() => {})