How to disable “prevent this page from creating additional dialogs”?

前端 未结 6 1524
一个人的身影
一个人的身影 2020-12-02 20:28

I\'m developing a web app that utilises JavaScript alert() and confirm() dialogue boxes.

How can I stop Chrome from showing this checkbox?<

相关标签:
6条回答
  • 2020-12-02 20:56
    function alertWithoutNotice(message){
        setTimeout(function(){
            alert(message);
        }, 1000);
    }
    
    0 讨论(0)
  • 2020-12-02 20:56

    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.

    I've found that an apps that has a lot of message boxes and confirms has a much better user experience if you use custom dialogs instead of the default alerts and confirms.

    0 讨论(0)
  • 2020-12-02 20:56

    You should better use jquery-confirm rather than trying to remove that checkbox.

    $.confirm({
        title: 'Confirm!',
        content: 'Are you sure you want to refund invoice ?',
        confirm: function(){
           //do something 
        },
        cancel: function(){
           //do something
        }
    }); 
    
    0 讨论(0)
  • 2020-12-02 21:05

    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):

    function myConfirm(message) {
        var start = Number(new Date());
        var result = confirm(message);
        var end = Number(new Date());
        return (end<(start+10)||result==true);
    }
    
    0 讨论(0)
  • 2020-12-02 21:17

    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.

    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:

    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);
    
    0 讨论(0)
  • 2020-12-02 21:18

    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.

    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.

    0 讨论(0)
提交回复
热议问题