2

I'm binding a simple click event to the window object but the handler doesn't get called in IE8 (works on Chrome and FF):

$(window).click(function (e) {
  alert('Hello there! I\'m in the window.click hanlder!');
});

Anyone why this is happening?

Gezim
  • 7,112
  • 10
  • 62
  • 98

1 Answers1

5

It seems that IE (testing IE8) doesn't bubble events to the window.

Here's an example ( http://jsfiddle.net/SZXrn/8/ ):

if (window.attachEvent) // IE
{
  window.attachEvent('onclick', function () {
      alert("Yay window obj was clicked! IE");
  });

  document.attachEvent('onclick', function () {
      alert("Yay document obj was clicked! IE");
  });
}
else if (window.addEventListener) // Other
{
  window.addEventListener('click', function () {
      alert("Yay window obj was clicked! Non-IE");
  });

  document.addEventListener('click', function () {
      alert("Yay document obj was clicked! Non-IE");
  });  
}

So, the solution is to bind to the document instead of window.

Gezim
  • 7,112
  • 10
  • 62
  • 98