jump to navigation

NOTE: The spam filter is being unusually aggressive. If you comment does not immediately appear, it has simply been placed in moderation and I will approve it as quickly as possible. Thank you for your patience.

"Murphy was an optimist!"

Today’s Technical Challenge November 26, 2007 4:58 pm

Posted by Doug McCaughan in : JavaScript, Programming, Technology
, trackback

This one sounds pretty simple. I bet you can’t do it! (That is coming from the guy who claims anything can be done…I will find a way!).

The challenge: Part 1- Write a piece of JavaScript that pops an alert box with the message "You left!" when focus is lost on the browser. This means that if someone is looking at the webpage in Firefox and then switches to another webpage in a different tab, different window, or goes to a different application altogether (say Microsoft Word or Internet Explorer), then the alert box should appear. Any loss of focus to the active webpage makes the alert popup.

The challenge: Part 2- Write a piece of JavaScript that pops an alert box with the message "You returned!" when focus is returned to the browser. This means if someone was checking their email, using Internet Explorer, or was in a different Firefox tab, when they return to your webpage the alert box pops up.

The JavaScript for this challenge should work in both Firefox and Internet Explorer. The user should be able to repeatedly remove focus and return to focus and see both messages multiple times.

Good luck!


Comments after advertisement


1. mikew - August 15, 2008

Did anyone ever solve this?

I can only surmise that Google chat accomplishes this using iframes. Perhaps the onfocus/onblur events behave in a more predictable/consistent fashion?