NNCCBB
NNCCBB

Reputation: 33

Javascript confirm closes instantly in chrome

This code is working correctly in firefox and IE but in chrome the javascript confirm window displays for just a second and instantly closes (and runs the else statement sending someone away from the page)

What is wrong with this code in 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/');
  }
}

Upvotes: 3

Views: 5680

Answers (1)

Jake Chasan
Jake Chasan

Reputation: 6550

This is expected behavioral for Google Chrome: Javascript : Alert Box dismissed when tabs switched. Each browser handles the confirm() dialog differently.

When you change the location of the window, the alert immediately dismisses. I would recommend writing your confirmation as an HTML node with a <button> element. This would prevent any discrepancies between different browsers.

Upvotes: 3

Related Questions