Forum Moderators: open

Message Too Old, No Replies

managing object focus change

         

warp_fr

10:46 am on Jan 26, 2005 (gmt 0)

10+ Year Member



I want a popup window to close if it looses focus, but window.onblur event will fire too if I click any link or elements inside that popup. I found this work around that seems to work perfect, let me know what you think about it, is it the right way to do it?
Too bad IE didn't implemented "document.onfocus", so I have to iterate through all document elements.
I added this javascript at the end of my popup source code :
var stopIt;
window.onblur = function(evt){stopIt = setTimeout('self.close()',0);}
document.onfocus = function(etv){clearTimeout(stopIt);} // Mozilla only
for(var i=0; i<document.all.length; i++){ // IE
document.all[i].onfocus = function(etv){clearTimeout(stopIt);}
}

orion_rus

8:31 am on Jan 27, 2005 (gmt 0)

10+ Year Member



Can you post here all ur code, because it seems not to work. Also i didn't here what IE not using onfocus event, i think it's not true. If u give us all code, it would be much better i think
Good luck to you

warp_fr

2:02 pm on Jan 27, 2005 (gmt 0)

10+ Year Member



It's for testing with IE and Mozilla.
I have to iterate throught all document.all items to watch for object gaining focus in IE as onfocus (either onblur) isn't implemented at level document, in IE.
You can't just test with document.onfocus like in Mozilla.
If you want to give it a try, just make 5 simple html pages like those :

index.html
-----------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Main page</title>
</head>
<body>
We want the popup window opened to close itself if it loses focus but only when window is hidden or minimized :<br>
<a href="popupA.html" target="popupA" onClick="window.open('popupA.html', 'popupA'); return false;">popupA (without onfocus event handler, either IE or Mozilla)</a><br>
<a href="popupB.html" target="popupB" onClick="window.open('popupB.html', 'popupB'); return false;">popupB (with onfocus event handler, only for IE)</a><br>
<a href="popupC.html" target="popupC" onClick="window.open('popupC.html', 'popupC'); return false;">popupC (with onfocus event handler, only Mozilla)</a><br>
<a href="popupD.html" target="popupD" onClick="window.open('popupD.html', 'popupD'); return false;">popupD (with onfocus event handler, IE and Mozilla)</a><br>
</body>
</html>
-----------------

popupA.html
-----------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>popupA</title>
</head>
<body>
popupA (without onfocus event handler, either IE or Mozilla)<br>
Clicking anything in popupA will make window self close<br>
<a href="#">link</a>
<script language="JavaScript">
<!--
window.onblur = function(evt){self.close();}
//-->
</script>
</body>
</html>
-----------------

popupB.html
-----------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>popupB</title>
</head>
<body>
popupB (with onfocus event handler, only for IE)<br>
Click the link or in the page, window won't lose focus in IE<br>
<a href="#">link</a>
<script language="JavaScript">
<!--
var stopIt;
window.onblur = function(evt){stopIt = setTimeout('self.close()',0);}
for(var i=0; i<document.all.length; i++){ // IE
document.all[i].onfocus = function(etv){clearTimeout(stopIt);}
}
//-->
</script>
</body>
</html>
-----------------

popupC.html
-----------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>popupC </title>
</head>
<body>
popupC (with onfocus event handler, only for Mozilla)<br>
Click the link or in the page, window won't lose focus in Mozilla<br>
<a href="#">link</a>
<script language="JavaScript">
<!--
var stopIt;
window.onblur = function(evt){stopIt = setTimeout('self.close()',0);}
document.onfocus = function(etv){clearTimeout(stopIt);} // Mozilla
//-->
</script>
</body>
</html>
-----------------

popupD.html
-----------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>popupD </title>
</head>
<body>
popupD (with onfocus event handler, IE and Mozilla)<br>
Click the link or in the page, window won't lose focus either in Mozilla or IE<br>
<a href="#">link</a>
<script language="JavaScript">
<!--
var stopIt;
window.onblur = function(evt){stopIt = setTimeout('self.close()',0);}
document.onfocus = function(etv){clearTimeout(stopIt);} // Mozilla
for(var i=0; i<document.all.length; i++){ // IE
document.all[i].onfocus = function(etv){clearTimeout(stopIt);}
}
//-->
</script>
</body>
</html>
----------------

Bernard Marx

2:21 pm on Jan 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've been having a mess around on this problem, and so far I can say no more than that I agree with you. The only solution seems to be yours - or a variation thereof..

1. I don't think you need to add onfocus listeners to ALL elements on the page. The only elements that can receive focus are form elements {input, button, select etc}.

2. For a touch of efficiency, whether adding to all elements, or just form elements, it's better {speed & memory} to define a function separately, then make the assignents with a reference to that, rather than using n anonymous functions. Small matter in most cases though.