3

I've run into a small problem while detecting when user leaves the window in IE8. I am aware that addEventListener method is supported only in IE9+ versions so I came up with this solution:

function popUp() {
    console.log("i'm leaving")
}

if (window.addEventListener) {
    window.addEventListener("mouseout", popUp);
} else {
    window.attachEvent("mouseout", popUp);
}

It works properly in all major browsers, but still fails in IE8. I hoped that using jQuery will solve the problem:

$(window).mouseout(function(){
    popUp()
})

but, due to this information, neither mouseover nor mouseout events work on the window in IE8.

So the question is, how can I make it work in this quite obsolete, but unfortunately still popular browser? Any help would be really appreciated

Bart Az.
  • 1,644
  • 1
  • 22
  • 32
  • possible duplicate of [addEventListener not working in IE8](http://stackoverflow.com/questions/9769868/addeventlistener-not-working-in-ie8) – MarsOne Oct 14 '14 at 09:42
  • I've seen the question @MarsOne provided, but mine doesn't concern whether addEventListener works in IE8 or not, I know it doesn't. I just need to find a workaround for the fact, that one can not attach an mouseout, onmouseout or mouseleave to the window object in IE8 – Bart Az. Oct 14 '14 at 09:52

2 Answers2

0
window.attachEvent("onmouseout", popUp);

you lost 'on'

cxtom
  • 31
0

Back when these browsers where up-to-the-minute (a long time ago) the only way to get a triggered event for the pointer being outside of the browser was to wait for a blur event (which involves the user clicking outside):

window.attachEvent
  ? window.attachEvent('onblur', method)
  : window.addEventListener('blur', method);

http://www.quirksmode.org/dom/events/blurfocus.html

Or to use Flash which did actually have this capability — at least for going outside the stage.

The fallback JavaScript method (or at least the one I know about) was to have a thin border just around the inside of the window frame, and if the mouse moved over this region it would be counted as outside. It wasn't infallible however, if the mouse moved too quickly this region would be skipped, and the faux "mouseout" event would not be triggered.

+-----------------------------+
| +-------------------------+ |
| |                         | |
| |                         | |
| |     NOT ACTUAL SIZE *   | |
| |                         | |
| |                         | |
| +-------------------------+ | <-- border region
+-----------------------------+

* Any reference to an ascii representation of a screen, living or dead, is purely coincidental.

So not ideal, but neither are these archaic browsers — at least with contemporary eyes.

Pebbl
  • 34,937
  • 6
  • 62
  • 64