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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Closing a pop-up window when it loses focus

Status
Not open for further replies.

DrDrew

Programmer
Jun 24, 2002
8
US
My problem is I want a popup window to close when it loses 'focus'. I tried using the Self.Close() method which works great for closing the window, but I have Form Fields on the page and when I click inside a form field it closes the window because it treats it as losing focus (which I do not want to happen, I only want the pop-up window to close when the user clicks off of it). Anyone have any solutions or suggestions?
 
it's not foolproof, as different browsers have different ideas of when the window loses focus, but try this:

Code:
<script type=&quot;text/javascript&quot;>
window.onload = function attachEvents() {
	var els = document.getElementsByTagName(&quot;*&quot;);
	for (var x = 0; x < els.length; x++) {
		els[x].onfocus = function() {clearTimeout(window.timer)};
	}
	document.onblur = closeWindow;
	document.body.onblur = closeWindow;
}

window.onblur = function closeWindow() {
	window.timer = setTimeout(&quot;window.close();&quot;,1000);
}
</script>
=========================================================
while (!succeed) try();
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top