问题描述
我们网站的一些用户报告显示确认
对话框,但会立即消失,就像他们自动被解雇一样。这似乎只影响Chrome,而不是其他浏览器(甚至不是Chromium)。
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).
搜索类似问题发现很多人抱怨确认
onbeforeunload
中的对话框,但这不是我的问题:在这种情况下,不是。页面最初加载时显示 confirm
对话框(由jQuery $(document).ready()
)触发。
Searching for similar issues finds many people complaining 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()
).
显示 confirm
不会激活其标签,并且会在切换标签时将其解除。没关系:选项卡已经处于活动状态(页面加载时会出现确认
对话框),我很高兴在切换选项卡时将其解除。问题是它在没有任何用户交互的情况下立即被解雇。
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/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";
}
});
如有必要,我们可以使用jQuery模态窗口,但是使用整个库来替换一行代码似乎很愚蠢。无论如何,本机浏览器警报往往在移动浏览器中看起来更好。
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.
推荐答案
我有完全相同的问题。这似乎是一个问题。
I had exactly same issue. It seems like a chrome issue.
这需要一个技巧。
在我的情况下,它使用setTimeout函数延迟0.1秒。
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/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);
});
这篇关于Chrome在没有任何用户交互的情况下立即解除confirm()提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!