Chrome在没有任何用户干预的情况下立即取消了Confirm()提示

TRi*_*RiG 5 javascript confirm google-chrome

我们网站的一些用户报告说,该confirm对话框出现了,但立即消失了,就像它们被自动关闭一样。这似乎仅影响Chrome,不影响其他浏览器(甚至不影响Chromium)。

搜索相似的问题,发现许多人抱怨confirm内的对话框onbeforeunload,但这不是我的问题:这不是这种情况。confirm当页面最初加载时(由jQuery触发$(document).ready())显示该对话框。

浏览器的文件显示,confirm当标签从切换不会激活它的标签,将被开除。很好:该标签页已经处于活动状态(confirm对话框在页面加载时显示),并且很高兴在切换标签页后将其关闭。问题是它被立即关闭,没有任何用户交互。

我找到了一个类似的报告,但是在那种情况下,confirm提示从来没有出现过。看来我们所看到的有所不同。

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

如果需要,我们可以使用jQuery模态窗口,但是使用整个库替换一行代码似乎很愚蠢。无论如何,本机浏览器警报往往在移动浏览器中看起来更好。

小智 6

我有完全一样的问题。这似乎是一个镀铬的问题。

这需要技巧。就我而言,它通过使用setTimeout函数设置0.1秒的延迟来工作。

尝试这个。它会工作。

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

$(document).ready(function() {
  setTimeout(function(){ doConfirm() }, 100);    
});
Run Code Online (Sandbox Code Playgroud)


soc*_*efs 5

就我而言,问题是由 iframe 引起的。我有一个 youtube 视频 iframe,显示后几毫秒就会关闭警报。删除 iframe 后,一切正常。