最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Detect "prevent this page from creating additional dialogs" - Stack Overflow

programmeradmin2浏览0评论

QUESTION

How can I detect if a user has checked the box, "prevent this page from creating additional dialogs"?

WHY It's a problem

If the user has prevented the appearance of confirm boxes, the function confirm('foobar') always returns false.

If the user cannot see my confirmation dialogue boxes confirm('Are you sure?'), then the user can never perform the action.

CONTEXT

So, I use the code like if(confirm('are you sure?')){ //stuff... }. So an auto-response of false from the browser will prevent the user from ever doing stuff. But, if there was a way to detect that the user has checked the box, then I could execute the action automatically.

I think that if the user has disabled the dialogues, then the function should either throw an error, or return true. The function is meant to confirm an action that the user has requested.

QUESTION

How can I detect if a user has checked the box, "prevent this page from creating additional dialogs"?

WHY It's a problem

If the user has prevented the appearance of confirm boxes, the function confirm('foobar') always returns false.

If the user cannot see my confirmation dialogue boxes confirm('Are you sure?'), then the user can never perform the action.

CONTEXT

So, I use the code like if(confirm('are you sure?')){ //stuff... }. So an auto-response of false from the browser will prevent the user from ever doing stuff. But, if there was a way to detect that the user has checked the box, then I could execute the action automatically.

I think that if the user has disabled the dialogues, then the function should either throw an error, or return true. The function is meant to confirm an action that the user has requested.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jul 19, 2012 at 23:57 hrdwdmrblhrdwdmrbl 5,2693 gold badges35 silver badges47 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 18

As far as I know, this is not possible to do in any clean way as it's a browser feature, and if the browser doesn't let you know then you can't know.

However, what you could do is write a wrapper around confirm() that times the response time. If it is too fast to be human then the prompt was very probably suppressed and it would return true instead of false. You could make it more robust by running confirm() several times as long as it returns false so the probability of it being an über-fast user is very low.

The wrapper would be something like this:

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;
}

PS: if you want a practical solution and not this hack, Jerzy Zawadzki's suggestion of using a library to do confirmation dialogs is probably the best way to go.

I think you cannot change it - it's browser feature.

My first idea if you need workaround would be to change code from using system confirm to some js library alert (from e.g. jQuery UI)

发布评论

评论列表(0)

  1. 暂无评论