faqts : Computers : Programming : Languages : JavaScript : Windows

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

8 of 9 people (89%) answered Yes
Recently 8 of 9 people (89%) answered Yes

Entry

How can I set focus to the window when the user moves the mouse over it?

Sep 23rd, 2001 03:38
Martin Honnen,


You can set up a 
  document.onmouseover 
handler to call 
  window.focus()
which will then fire when the user moves the mouse over the content area 
of the window.
You need however to be careful to call window.focus() only when the 
mouse comes from the outside of the window.
With IE4+ you can check
  if (!event.fromElement)
With NN6 you should be able to check
  if (!evt.relatedTarget)
where evt is the argument of you onmouseover handler. However I can't 
get the NN6 code to work as intended therefore the above example is IE4+ 
only:
<html>
<head>
<title>
window activation onmouseover
</title>
<script type="text/javascript">
document.onmouseover = function (evt) {
  if (document.all && !event.fromElement)
    window.focus();
}
</script>
</head>
<body>
Kibology
</body>
</html>