我正在开发一个使用 JavaScript
alert()
和 confirm()
对话框的 Web 应用程序。
如何阻止 Chrome 显示此复选框?
有我可以修改的设置吗?
我知道我可以修改源代码,但我希望这样 Chrome 仍然可以自动更新。
我不需要担心其他浏览器,因为该应用程序仅在 Chrome 中运行。
我对运行该应用程序的 (Windows) 计算机具有管理员访问权限。
你不能。这是一个浏览器功能,可以防止网站显示数百个警报,从而阻止您离开。
但是,您可以查看模式弹出窗口,例如 jQuery UI 对话框。这些是显示自定义对话框的 JavaScript 警报框。他们不使用默认的
alert()
功能,因此完全绕过您遇到的问题。
我发现,如果您使用自定义对话框而不是默认的警报和确认,那么具有大量消息框和确认的应用程序将具有更好更好的用户体验。
这就是我最终所做的,因为我们有一个网络应用程序,其中有多个不受我们控制的用户......(@DannyBeckett 我知道这不是您问题的确切答案,但正在寻找的人你的问题可能会因此得到帮助。)你至少可以检测到他们是否没有看到对话框。您最可能想要更改的内容很少,例如显示时间或实际显示的内容。请记住,这只会通知用户他们已成功单击该小复选框。
window.nativeAlert = window.alert;
window.alert = function (message) {
var timeBefore = new Date();
var confirmBool = nativeAlert(message);
var timeAfter = new Date();
if ((timeAfter - timeBefore) < 350) {
MySpecialDialog("You have alerts turned off");
}
}
window.nativeConfirm = window.confirm;
window.confirm = function (message) {
var timeBefore = new Date();
var confirmBool = nativeConfirm(message);
var timeAfter = new Date();
if ((timeAfter - timeBefore) < 350) {
MySpecialDialog("You have confirms turned off");
}
return confirmBool;
}
显然我把时间设置为3.5毫秒。但经过一些测试后,我们只能在大约 5 毫秒以上的时间内单击或关闭对话框。
我知道每个人在道德上都反对这样做,但我知道在需要这样做的地方有一些恶作剧的理由。我认为 Chrome 对此采取了坚定的立场,在警报消息之间强制执行一秒的间隔时间。如果访问者被困在恼人的恶作剧网站上,这将为访问者提供足够的时间来关闭页面或刷新。
所以回答你的问题,只是时间问题。如果您每秒提醒一次以上,Chrome 将创建该复选框。这是解决方法的一个简单示例:
var countdown = 99;
function annoy(){
if(countdown>0){
alert(countdown+" bottles of beer on the wall, "+countdown+" bottles of beer! Take one down, pass it around, "+(countdown-1)+" bottles of beer on the wall!");
countdown--;
// Time must always be 1000 milliseconds, 999 or less causes the checkbox to appear
setTimeout(function(){
annoy();
}, 1000);
}
}
// Don't alert right away or Chrome will catch you
setTimeout(function(){
annoy();
}, 1000);
您应该更好地使用 jquery-confirm 而不是尝试删除该复选框。
$.confirm({
title: 'Confirm!',
content: 'Are you sure you want to refund invoice ?',
confirm: function(){
//do something
},
cancel: function(){
//do something
}
});
您无法阻止禁用确认复选框,正如其他人回答它的浏览器功能中所述。
但是,从问题的字里行间阅读,如果用户确实禁用确认对话框,您似乎可能不想破坏功能。
一个简单的解决方案是建立一个确认,禁用时默认为 true。
即如果用户禁用确认对话框,我们假设这意味着用户不想确认,而不是用户想要拒绝可能要求确认的所有内容。
// web/src/utils/blocked-to-true-confirm.js
/**
* Simple wrapper around window.confirm that returns true if the
* confirm dialog is blocked.
*/
export function blockedToTrueConfirm(message) {
const startTime = Date.now()
const result = window.confirm(message)
const endTime = Date.now()
// If the confirm returns faster than a human could possibly click
// (e.g., in 1 millisecond), assume it's been blocked
if (endTime - startTime < 1) {
return true
}
return result
}
export default blockedToTrueConfirm
用法会是这样的
async function deleteHotel(hotel) {
if (!blockedToTrueConfirm("Are you sure you want to remove this hotel?")) return
try {
await travelDeskHotelsApi.delete(hotel.id)
await refresh()
} catch (error) {
console.error(error)
}
}
例如https://github.com/icefoganalytics/travel-authorization/commit/df86563b6824c2f93d025123aa0a8fd640bc5501
你的问题是你需要知道他们已经同意,然后假设他们的意思是“确定”,而不是“取消”。将
confirm(x)
替换为
myConfirm(x)
:
function myConfirm(message) {
var start = Number(new Date());
var result = confirm(message);
var end = Number(new Date());
return (end<(start+10)||result==true);
}
function alertWithoutNotice(message){
setTimeout(function(){
alert(message);
}, 1000);
}