Search code examples
javascripteventsgoogle-chromeonfocus

window.onfocus doesn't stop firing in Chrome


The window.onfocus event does not stop firing in Chrome. It is fine in every other browser. Here is my code:

window.onfocus = function() {
  alert('focused');
}

What is the best way to fix this?


Solution

  • That's the expected behavior because what's happening is that the alert makes the window object loose focus, and after closing the alert the focus is returned to the window object, firing the event again.