【问题标题】:Javascript confirm closes instantly in chromeJavascript 确认在 chrome 中立即关闭
【发布时间】:2018-06-27 14:16:35
【问题描述】:

此代码在 firefox 和 IE 中正常工作,但在 chrome 中,javascript 确认窗口仅显示一秒钟并立即关闭(并运行 else 语句将某人从页面中发送出去)

Chrome 中的这段代码有什么问题?

var alerted = '';

if (alerted != 'yes') {
  if (confirm("Accept?")) {
    localStorage.setItem('alerted', 'yes');
    //box.checked = true;
  }
  else {
    window.location.assign('https://www.example.com/previous-page/');
  }
}

【问题讨论】:

  • 你确定 cuz 正在和我一起工作,只是在是的情况下不给框声明
  • 在 Win 10 上的当前 Chrome (67.0.something) 中也适用于我。
  • 在 x64 Windows 系统上使用 Chrome 版本 67.0.3396.87(官方构建)(64 位)会出现一个等待我输入的确认框。
  • This 可能有用。它是 chrome 如何在现代浏览器中处理 confirm 的。值得注意的是关于非焦点选项卡上的弹出窗口如何立即关闭。可能不适用,但值得注意。无论哪种方式confirms/prompts/alerts等都应该避免。
  • 请在此处测试,并添加可能导致您的问题的代码。在这种情况下,它似乎无法重现。

标签: javascript google-chrome confirm window.location


【解决方案1】:

这是 Google Chrome 的预期行为: Javascript : Alert Box dismissed when tabs switched。每个浏览器处理confirm() 对话框的方式不同。

当您更改窗口的位置时,警报会立即消失。我建议将您的确认写为带有<button> 元素的HTML 节点。这将防止不同浏览器之间的任何差异。

【讨论】:

    猜你喜欢
    • 2022-07-30
    • 2014-06-14
    • 2010-11-27
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多