Javascript 如何禁用“阻止此页面创建其他对话框”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11729835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable "prevent this page from creating additional dialogs"?
提问by Danny Beckett
I'm developing a web app that utilises JavaScript alert()
and confirm()
dialogue boxes.
我正在开发一个使用 JavaScriptalert()
和confirm()
对话框的网络应用程序。
How can I stop Chrome from showing this checkbox?
如何阻止 Chrome 显示此复选框?
Is there a setting I can modify?
有我可以修改的设置吗?
I know I could modify the source code, but I'd like it so that Chrome could still auto-update.
我知道我可以修改源代码,但我希望 Chrome 仍然可以自动更新。
I don't need to worry about other browsers since the app only runs in Chrome.
我不需要担心其他浏览器,因为该应用程序只能在 Chrome 中运行。
I have admin access to the (Windows) machines that run the app.
我对运行该应用程序的 (Windows) 计算机具有管理员访问权限。
采纳答案by Tomasz Mazur
function alertWithoutNotice(message){
setTimeout(function(){
alert(message);
}, 1000);
}
回答by sachleen
You can't. It's a browser feature there to prevent sites from showing hundreds of alerts to prevent you from leaving.
你不能。这是一个浏览器功能,可防止站点显示数百个警报以防止您离开。
You can, however, look into modal popups like jQuery UI Dialog. These are javascript alert boxes that show a custom dialog. They don't use the default alert()
function and therefore, bypass the issue you're running into completely.
但是,您可以查看像jQuery UI Dialog这样的模式弹出窗口。这些是显示自定义对话框的 javascript 警报框。他们不使用默认alert()
功能,因此可以完全绕过您遇到的问题。
I've found that an apps that has a lot of message boxes and confirms has a muchbetter user experience if you use custom dialogs instead of the default alerts and confirms.
我发现如果您使用自定义对话框而不是默认警报和确认,那么具有大量消息框和确认的应用程序的用户体验会更好。
回答by DeadlyChambers
This is what I ended up doing, since we have a web app that has multiple users that are not under our control...(@DannyBeckett I know this isn't an exact answer to your question, but the people that are looking at your question might be helped by this.) You can at least detect if they are not seeing the dialogs. There are few things you most likely want to change like the time to display, or what you are actually displaying. Remember this will only notify the user that they are have managed to click that little checkbox.
这就是我最终要做的,因为我们有一个网络应用程序,其中有多个不受我们控制的用户...(@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;
}
Obviously I have set the time to 3.5 milliseconds. But after some testing we were only able to click or close the dialogs in about 5 milliseconds plus.
显然我已将时间设置为 3.5 毫秒。但是经过一些测试,我们只能在大约 5 毫秒以上的时间内单击或关闭对话框。
回答by CauselessEffect
I know everybody is ethically against this, but I understand there are reasons of practical joking where this is desired. I think Chrome took a solid stance on this by enforcing a mandatory one second separation time between alert messages. This gives the visitor just enough time to close the page or refresh if they're stuck on an annoying prank site.
我知道每个人在道德上都反对这一点,但我理解在需要的地方开实际玩笑是有原因的。我认为 Chrome 通过在警报消息之间强制执行强制性的一秒间隔时间对此采取了坚定的立场。这让访问者有足够的时间关闭页面或刷新,如果他们被困在一个恼人的恶作剧网站上。
So to answer your question, it's all a matter of timing. If you alert more than once per second, Chrome will create that checkbox. Here's a simple example of a workaround:
所以要回答你的问题,这只是时间问题。如果您每秒提醒多次,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);
回答by vvvvvvvvvv
You should better use jquery-confirmrather than trying to remove that checkbox.
您最好使用jquery-confirm而不是尝试删除该复选框。
$.confirm({
title: 'Confirm!',
content: 'Are you sure you want to refund invoice ?',
confirm: function(){
//do something
},
cancel: function(){
//do something
}
});
回答by Ian
You should let the user do that if they want (and you can't stop them anyway).
如果用户愿意,您应该让他们这样做(无论如何您都无法阻止他们)。
Your problem is that you need to know that they have and then assume that they mean OK, not cancel. Replace confirm(x)
with myConfirm(x)
:
你的问题是你需要知道他们有,然后假设他们的意思是好的,而不是取消。替换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);
}