Click here to Skip to main content
15,917,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii,

I need to prevent scrolling when mouse wheel is clicked.
The preventDefault() methos is working in Chrome but its not working in firefox.
Any help?

here is my code:


C#
$(document).bind('mousedown', function (e) {
            if ((e.which == 2)) {
                  
                e.preventDefault();
            }
        });
Posted
Comments
Member 10863532 4-Jun-14 7:56am    
Browser Tech Support For Free @ 1800 935 0537

1 solution

The mousedown and click events occur together and are not sharing the same event object.

Calling e.preventDefault(); in the mousedown event is not able to prevent the default click behaviour on the click event.

Why it works for you in Chrome is most likely a different reason or a even false positive.

To Demonstrate, assuming you have the following HTML:
HTML
<a href="http://jsfiddle.net/" id="whichkey">link to google</a>
<div id="log"></div>


The following Script will not prevent the default behaviour and clicking the link will always work:
JavaScript
$("#whichkey").on("mousedown", function (event) {
    $("#log").html(event.type + ": " + event.which);
    event.preventDefault(); // won't work!
    //return false; // won't work either!
});


However, if you add the following script to the above, then it will work.
JavaScript
$("#whichkey").on("click", function (event) {
    event.preventDefault(); // will work!
});


See this jsFiddle to demonstrate and for you to play around with:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900