Ah, yes.. i see what you mean. That's really wierd actually, never noticed that before with the onfocus event. After going to Yahoo, and then hitting the back button the browser.. the window still has focus, but the event doesn't fire. I even tried blurring the page and then giving it focus again and that didn't work either.
I played around with it, and come to think of it for your situation window.onload would probably be better since it fires any time the page gets displayed on your screen.
-
<html>
-
<head>
-
<script>
-
window.onload = myfunc;
-
function myfunc() {
-
alert('window has focus!')
-
}
-
</script>
-
</head>
-
</head>
-
<body bgcolor="lightgrey">
-
focus test<P>
-
<a href="http://www.yahoo.com" target=_self>Yahoo</a>
-
</body>
-
</html>
-
This will only occur when someone first visits the page, or goes back to it in the browser. If you want to continually update the page (say every x seconds/minutes) like you described yahoo does.. you can put a setTimeout on it.
Replace the test function with have above with this...
-
function myfunc() {
-
alert('window has focus!')
-
window.setTimeout("myfunc()",1000)
-
}
-
That will cause the page to pop up an alert box every second.
The code will automatically stop execution when you browse away from the page because you're simply.. not on that page anymore. When you go back to the page (you're firing the onload event again) it starts over again.
Try it.. it (very annoyingly) worked for me.