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

window.onfocus problem with IE

Status
Not open for further replies.

steph2004

Programmer
Apr 1, 2004
7
CA
Hi,

I'm having a javascript calling two functions when the focus and blur event occur. These are set like this:

window.onfocus = setFocusOn;
window.onblur = setFocusOff;

My script is in a .js file and I refer to it via the <script src="functions.js"> tag.

My problem is that if I am on the page and then go on MSN messenger per example, the focus is lost, which is ok. Then, if I click on the task bar to get back on the page, the focus is gained. But if I click inside the web page, on the document, the focus is gained and lost just after that! I'm still on the we page and the focus is gone somewhere I don't know...!

It works fine on NS 7. If I click on the web page, the focus is gained. But in IE, the focus is gained and then lost just after every click I do in the page...

Any idea of what's going on??

Thanks

Stephane
 
I wonder if the document.body and window share the events. Could it be that it's being caused by the focus being moved from the window to the body? What happens if you use this:
Code:
document.body.onfocus = setFocusOn;
document.body.onblur = setFocusOff;

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Wait, it probably depends on what you're doing in your setFocusOn and setFocusOff functions. What do they do?

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Ok, I did a little experiment with the following code and found that clicking on the link caused the window to lose focus since the link was gaining focus:
Code:
<html>
<head>
<script>
window.onfocus=dofocus;
window.onblur=doblur;
function dofocus(){
  document.body.innerHTML+='<br>window is focused'
}
function doblur(){
  document.body.innerHTML+='<br>window is blurred'
}
</script>
</head>
<body>
<a href="" onclick="return false">test</a>

</body>
</html>
After a little research, I found this on Microsoft's website:
Microsoft said:
For Internet Explorer 5.5 and later, focus on a document, and the active element of a document can be managed separately. The synchronous events onactivate and ondeactivate provide better control for managing activation changes.

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Hi,

Thanks for your time, It helped me. I resolved it by using the document.onclick event which is giving the focus to the window.

Stephane
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top