how to know whether modal boxes (alert, prompt, confirm…) have been disabled in javascript?

后端 未结 2 879
逝去的感伤
逝去的感伤 2020-12-03 15:13

I have a private web based app where sometimes I genuinely ask to the users what they want to do in given situations. To do so, I\'m using the confirm function

相关标签:
2条回答
  • 2020-12-03 15:49

    When that box is checked, the dialog "closes" immediately. You could check to see if the box closes unusually fast:

    function dialog(message, success, failure) {
        var open_time = new Date();
        var result = alert(message);
        var close_time = new Date();
    
        if (close_time - open_time < 10) {
            failure();
        } else {
            success(result);
        }
    }
    
    dialog('Hello', function(result) {
        // The dialog probably was closed by the user
    }, function() {
        // The dialog was closed really fast.
        // Either the user was typing while it popped up or the browser didn't
        //  display it in the first place
    });
    

    Although just using CSS and HTML to create modal dialogs would probably be much easier and more consistent across browsers and platforms. I personally don't like Chrome's approach.

    Demo: http://jsfiddle.net/tS9G6/4/

    I looked a little bit through Chromium's source and that property isn't stored anywhere, so there doesn't seem to be some Chromium-specific property that you can look at.

    0 讨论(0)
  • 2020-12-03 15:58

    You can't do anything about it. It's a browser feature.

    You can check for the timing -

    How to Detect "prevent this page from creating additional dialogs"

    This suggests doing the following :

    function myConfirm(message){
        var start = new Date().getTime();
        var result = confirm(message);
        var dt = new Date().getTime() - start;
        // dt < 50ms means probable computer
        // the quickest I could get while expecting the popup was 100ms
        // slowest I got from computer suppression was 20ms
        for(var i=0; i < 10 && !result && dt < 50; i++){
            start = new Date().getTime();
            result = confirm(message);
            dt = new Date().getTime() - start;
        }
        if(dt < 50)
           return true;
        return result;
    }
    
    0 讨论(0)
提交回复
热议问题