Nocklas
Nocklas

Reputation: 1367

jQuery mousedown event not firing for window in IE8

I'm having problems hooking up the mousedown event for the window using jQuery in IE8. I'm getting no errors, but the event does not seem to be firing. It does work in IE9 and all other browsers I have tried. Here's my code:

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function test(e) {
            alert('test');
        }

        $(document).ready(function () {
            $(window).mousedown(test);
        });     
    </script>   
</head>
<body>   
</body>
</html>

Upvotes: 1

Views: 5678

Answers (2)

John
John

Reputation: 539

The problem is that you are using the global window.event object, and not jQuery's event object. window.event only works in some browsers, and it is not a W3C standard.

jQuery normalizes the event object so it's the same in all browsers. The event handler is passed that jQuery event object as a parameter. You should be using that.

 $(".class_name").mousedown(function (e) {

  switch (e.which) {
    case 1: //leftclick
        //...
        break;
    case 3: //rightclick
        //...
        break;
  }
});

Upvotes: 1

Chamika Sandamal
Chamika Sandamal

Reputation: 24332

use document instead of window

$(document).ready(function() {
    $(document).mousedown(function() {
        alert('test');
    });
});

Upvotes: 5

Related Questions