javascript - 如何修改我的代码以在所有类型的浏览器中执行?

标签 javascript google-chrome firefox

我想在用户关闭浏览器时显示带有个人消息的弹出窗口 我发现了很多类似的脚本,但它们在 Firefox 中工作,而不是在 Chrome 中工作, 有什么办法解决这个问题吗?

    window.onbeforeunload = function (event) {
    var message = 'Important: Please click on \'Save\' button to leave this page.';
    if (typeof event == 'undefined') {
        event = window.event;
    }
    if (event) {
        event.returnValue = message;
    }
    return message;
};

此外,我的代码中没有该消息,但我有另一条消息,如下所示:“Cette pagedemande de recognizer sa fermeture ; des données saisies pourraient ne pas être enregistrées”

最佳答案

根据https://developers.google.com/web/updates/2016/04/chrome-51-deprecations?hl=en#remove_custom_messages_in_onbeforeload_dialogs的信息

A window’s onbeforeunload property may be set to a function that returns a string that is shown to the user in a dialog box to confirm that the user wants to navigate away. This was intended to prevent users from losing data during navigation. Unfortunately, it is often used to scam users.

Starting in Chrome 51, a custom string will no longer be shown to the user. Chrome will still show a dialog to prevent users from losing data, but it's contents will be set by the browser instead of the web page.

With this change, Chrome will be consistent with Safari 9.1 and later, as well as Firefox 4 and later.

这意味着您无法在 onbeforeunload 事件中获取自定义消息。如果确实是提醒用户退出前保存,默认的浏览器消息基本上应该说相同的信息(并且将基于用户的浏览器语言),因此您不需要手动实现此功能。

关于javascript - 如何修改我的代码以在所有类型的浏览器中执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40026161/

相关文章:

javascript - 计算数组的百分比

c# - 每次使用innerHTML或jquery html()渲染页面的一部分时如何防止页面滞后/挂起

javascript - 持久本地 JavaScript - Chrome 调试器

jquery - $ 在 Firefox 中没有定义

css - 在 Firefox 24 中支持 “border-radius”

javascript - 如何修复 promise i̶n̶ ̶F̶i̶r̶e̶f̶o̶x̶?

javascript - 在执行新函数之前,如何等待两个 DynamoDB 调用完成?

android - AudioContext() 在某些手机上不存在?

javascript - 如何防止 "The play() request was interrupted by a call to pause()"错误?

jquery - FireFox 不发送 CORS 请求(使用 jQuery)