This thread deals with closing a window when it loses focuse:
http://www.tek-tips.com/viewthread.cfm?qid=1029426&page=5
You could adapt the code for your use. Note the two different code paths -- one for IE, one for other browsers.
If, however, you need a more reliable solution, then you could...