Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

close pop-up window when it loses focus

Status
Not open for further replies.

davikokar

Technical User
May 13, 2004
523
IT
Hallo,
is it possible to automatically close a pop-up window when it loses the focus?
thanks for suggestions
 
What you could do is when you open the pop-up window set a global variable on the parent window so the page knows the pop-up window is open, then in the body tag of the parent window set the onFocus to goto a function which checks if the global variable is set and if so closes the pop-up window.

You can now breath.

hth

Stewart
 

Or, just add this to the body tag of the popup:

Code:
<body onblur="self.close();">

It works for me in at least FF1 and IE6 (with XP SP2).

Hope this helps,
Dan
 
BRPS-

This would, however, cause problems if there were any form field elements on the popup. Focusing on a field element will cause the onblur method of the body to be called.

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 

Good point - and unfortunately, moving the onblur handler from the body to the window makes it even worse, as the behaviour differs from browser to browser then.

One way around this would be to write an onblur handler for the body, which runs a setTimeout to close the window after, say, 250msecs.

All elements on the page could have their onfocus events pointing to a generic handler which simply cancelled the setTimeout. The onfocus handlers could be set for all elements quite easily with javascript.

I think that that would give the desired results.

Hope this helps,
Dan
 

Hmmm - I've been doing some playing with the idea, and it seems that, at least with IE, it is not be possible to do it with 100% "common sense factor" built in...

What I mean by this is that it would work most of the time, but there are a few things that don't seem possible to detect - primarily:

My idea above works fine when clicking around on elements within the window (that can have a focus handler attached), but when clicking between the window and the address bar (which we cannot harness), the window would still close.

Of course, it being a popup means that you may choose to open it without an address bar... If it does not have one, the idea is quite workable - although it is still possible that things like alert boxes, popup menus, other browser modifications, etc, which cannot have a focus will still cause the window to close.

After looking into it, I'd say that it probably isn't worth (a) the hassle, or (b) the chances of a false positive.

Hope this helps,
Dan


what we would class as being blurred is not really always detectable
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top