I am not sure why this is happening, and I would like an explanation.
Using the jquery focus
method, I snap to the window focus event. This is a working example (copy to html file and open it in a browser. For some reason, it does not work in jsfiddle or jsbin)
<!DOCTYPE html> <html> <head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script></head> <body> <p1>Here:</p1> <div id="here" >Why</div> </body> <script> $(window).load(function() { $(window).focus(function() {console.log("focus");}); $(window).blur(function() {console.log("blur");}); }); </script> </html>
When the browser regains focus, the function runs twice, and the "focus" is printed twice in the console. Any idea why this is happening?
The ultimate goal, by the way, is to stop the timer from starting when the user leaves the browser in an application or other tab.
UPDATE
Works on the latest (dev) version of chrome. I will check it on firefox and write if it is different there.
UPDATE 2 Interesting fact - does not happen in firefox. Perhaps this is a mistake with chrome.
source share