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

javascript - Chrome dismisses confirm() promps immediately without any user interaction - Stack Overflow

programmeradmin9浏览0评论

Some users of our website report that confirm dialog boxes appear, but immediately disappear, as if they are being dismissed automatically. This appears to be affecting Chrome only, not other browsers (not even Chromium).

Searching for similar issues finds many people plaining about confirm dialogs within onbeforeunload, but that is not my issue: this is not in that situation. The confirm dialog is shown when the page initially loads (triggered by jQuery $(document).ready()).

The Chrome documentation shows that confirm will not activate its tab and will be dismissed when the tab is switched from. That’s fine: the tab is already active (the confirm dialog appears on page load), and I am happy for it to be dismissed when the tab is switched from. The issue is that it’s being dismissed immediately, without any user interaction.

I found one similar report, but in that case the confirm prompts never appeared in the first place. It looks like what we’re seeing is something different.

$(document).ready(function() {
var c = confirm('Are you sure you wish to delete this entry?');
if (c) {
    $.ajax(
        '/api/show/petition/delete',
        {
            'method': 'POST',
            'data': { 'id' : 9 },
            'dataType': 'json',
            'plete': function(response, status) {
                if (response.responseJSON.error) {
                    alert(response.responseJSON.message);
                    window.location.reload();
                } else {
                    document.location.href = "/show/application/petition";
                }
            }
        }
    );
} else {
    document.location.href = "/show/application/petition/entry/9";
}
});

We could use a jQuery modal window if necessary, but it seems silly to use an entire library to replace one line of code. And native browser alerts tend to look better in mobile browsers anyway.

Some users of our website report that confirm dialog boxes appear, but immediately disappear, as if they are being dismissed automatically. This appears to be affecting Chrome only, not other browsers (not even Chromium).

Searching for similar issues finds many people plaining about confirm dialogs within onbeforeunload, but that is not my issue: this is not in that situation. The confirm dialog is shown when the page initially loads (triggered by jQuery $(document).ready()).

The Chrome documentation shows that confirm will not activate its tab and will be dismissed when the tab is switched from. That’s fine: the tab is already active (the confirm dialog appears on page load), and I am happy for it to be dismissed when the tab is switched from. The issue is that it’s being dismissed immediately, without any user interaction.

I found one similar report, but in that case the confirm prompts never appeared in the first place. It looks like what we’re seeing is something different.

$(document).ready(function() {
var c = confirm('Are you sure you wish to delete this entry?');
if (c) {
    $.ajax(
        '/api/show/petition/delete',
        {
            'method': 'POST',
            'data': { 'id' : 9 },
            'dataType': 'json',
            'plete': function(response, status) {
                if (response.responseJSON.error) {
                    alert(response.responseJSON.message);
                    window.location.reload();
                } else {
                    document.location.href = "/show/application/petition";
                }
            }
        }
    );
} else {
    document.location.href = "/show/application/petition/entry/9";
}
});

We could use a jQuery modal window if necessary, but it seems silly to use an entire library to replace one line of code. And native browser alerts tend to look better in mobile browsers anyway.

Share Improve this question edited Nov 14, 2018 at 15:08 TRiG asked Jul 9, 2018 at 16:48 TRiGTRiG 10.6k8 gold badges61 silver badges111 bronze badges 2
  • 1 Are you sure it is not a pop up blocker or chrome blocking it since no user action was triggered to show the prompt. – epascarello Commented Jul 9, 2018 at 16:55
  • In your link, the problem solved by removing some extensions and then restart the browser. Are you sure it isn't the problem in this issue (maybe they have ad blockers)? – Chayim Friedman Commented Jul 9, 2018 at 17:23
Add a ment  | 

5 Answers 5

Reset to default 9

I had exactly same issue. It seems like a chrome issue.

It requires a trick. In my case, it worked by putting 0.1 sec delay with setTimeout function.

Try this. It will work.

function doConfirm() {
  var c = confirm('Are you sure you wish to delete this entry?');
  if (c) {
    $.ajax(
        '/api/show/petition/delete',
        {
            'method': 'POST',
            'data': { 'id' : 9 },
            'dataType': 'json',
            'plete': function(response, status) {
                if (response.responseJSON.error) {
                    alert(response.responseJSON.message);
                    window.location.reload();
                } else {
                    document.location.href = "/show/application/petition";
                }
            }
        }
    );
  } else {
    document.location.href = "/show/application/petition/entry/9";
  }
}

$(document).ready(function() {
  setTimeout(function(){ doConfirm() }, 100);    
});

In my case, the problem was caused by an iframe. I had a youtube video iframe, and the alert would close few milliseconds after showing. After removing the iframe, everything worked fine again.

In my case the issue was appearing for confirm dialog in click event function. Replacing "click" by "mouseup" resolved the issue.

In my case, the Facebook Remarketing Pixel was the source of the problem.

A slightly more elegant but effective way of adding a timeout is to include a library like underscore.js and debounce the handler function by ~100ms. If you have an iFrame, it might not be the case but it's definitely an issue without it and this worked instantly for me.

$("#my-selector").on("click", _.debounce(handleClick, 100));
发布评论

评论列表(0)

  1. 暂无评论