I'm really looking for something that works with all current popular browsers (IE9, Chrome, Firefox, Safari) and all the way back to IE8.
Although, I've been looking for a way to set focus on a Flash move object after it has lost focus, I've found that all historical ways of doing this fail. I assume it is yet another security issue.
So, I'm now looking for how to monitor change events of some sort for the document.activeElement (though "change" doesn't really occur).
It seems that you can use a combination of capturing focus/blur and focusin/focusout (IE) events. Something like this perhaps:
onfocusout
will catch bubbled "blurs" while regularblur
onaddEventListener
will capture "blurs".You may need to add additional checks in
blurHappened
. I'm not sure as I haven't tested.While @James's answer above is correct. I've added more details to make it a completely working solution along with the use of
focus
event too.In my experience, the best source of information on cross-browser issues is Quirksmode. Here's a link to the "obvious" (but not useable) option -- focus and blur events.
http://www.quirksmode.org/dom/events/blurfocus.html
Oddly (and surprisingly) it's Webkit-based browsers that are the fly in the ointment here.
Another option would be to use an interval timer to check whether the activeElement has changed either as your sole option OR as backup for focus/blur. (You could also listen for key presses, mouse clicks, and touches to check if activeElement changes.) If you cover these options, your intervalTimer will hardly ever need to clean up.