7 Replies - 957 Views - Last Post: 16 September 2018 - 07:49 PM

#1 daredavel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 26-January 16

Disable Mouse Wheel Click in Chrome

Posted 16 September 2018 - 06:02 AM

Hello, I'm trying to disable mouse wheel click on 'href' link and the code below is working on firefox, but not in chrome?

if (document.layers) {
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown = function () {
        return false;
    };
}
else {
    document.onmouseup = function (e) {
        if (e != null && e.type == "mouseup") {    
            if (e.which == 2 || e.which == 3) {   
                return false;
            }
        }
    };
}
document.oncontextmenu = function () {
    return false;
};


How do I make it work in chrome? or any other way I can disable mouse wheel click in chrome? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Disable Mouse Wheel Click in Chrome

#2 daredavel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 26-January 16

Re: Disable Mouse Wheel Click in Chrome

Posted 16 September 2018 - 06:15 AM

I'm not sure if where's the right place to post this or if this is not, please move this to the appropriate room. Thank you!
Was This Post Helpful? 0
  • +
  • -

#3 Programmer2004   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 96
  • Joined: 25-October 17

Re: Disable Mouse Wheel Click in Chrome

Posted 16 September 2018 - 06:56 AM

I was experimenting and I found a solution for your problem.

First, create a global function like this:
function clickProcedure(e) {
   if (e.which == 2) {
      e.preventDefault();
   }
}



This function will stop the default action when middle button is pressed.
You don't have to worry about the value of e.which, because it will be always 2 when the middle button is pressed.

Now register the event:
document.addEventListener("click", clickProcedure); //This one will work on Firefox
document.addEventListener("auxclick", clickProcedure); //This one will work on Chrome



While the standard "click" works on both Firefox and Chrome, it doesn't work on Chrome when a link is clicked. So Chrome developers added "auxclick", which is fired regardless of the element you clicked.

Remember, register both "click" and "auxclick", because "auxclick" works only on Chrome.

Off-Topic: This question is related to Javascript, so it should be in Javascript section, not in PHP.
Was This Post Helpful? 2
  • +
  • -

#4 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Disable Mouse Wheel Click in Chrome

Posted 16 September 2018 - 07:11 AM

You could try :: <body oncontextmenu="return false;"> although I don't recommend doing this especially because it will piss off your users and likely you will lose visitors. Users like full control of webpages and disabling right click may rub many of your users up the wrong way. For what reason do you want to block right click on href?

You would be better off putting the oncontextmenu="return false;" in a canvas or something, rather than the body tag. That was just an example. You could also do ::

    $(function() {
        $(this).bind("contextmenu", function(e) {
            e.preventDefault();
        });
    }); 



But if this is not working in chrome, try target the mouse up and mouse down event handlers instead. As its more likely to work.
Was This Post Helpful? 0
  • +
  • -

#5 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Disable Mouse Wheel Click in Chrome

Posted 16 September 2018 - 07:23 AM

Sorry, I just re-read your post and, realised you want this for mouse wheel. You can use the above, which should still work, with a little tweaking. You can also do so using event attributes:
$('body').mousedown(function(e){if(e.button==1)return false});
but I recommend using event handlers unobtrusively for better practices.
Was This Post Helpful? 0
  • +
  • -

#6 daredavel   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 26-January 16

Re: Disable Mouse Wheel Click in Chrome

Posted 16 September 2018 - 07:50 AM

View PostProgrammer2004, on 16 September 2018 - 06:56 AM, said:

I was experimenting and I found a solution for your problem.

First, create a global function like this:
function clickProcedure(e) {
   if (e.which == 2) {
      e.preventDefault();
   }
}



This function will stop the default action when middle button is pressed.
You don't have to worry about the value of e.which, because it will be always 2 when the middle button is pressed.

Now register the event:
document.addEventListener("click", clickProcedure); //This one will work on Firefox
document.addEventListener("auxclick", clickProcedure); //This one will work on Chrome



While the standard "click" works on both Firefox and Chrome, it doesn't work on Chrome when a link is clicked. So Chrome developers added "auxclick", which is fired regardless of the element you clicked.

Remember, register both "click" and "auxclick", because "auxclick" works only on Chrome.

Off-Topic: This question is related to Javascript, so it should be in Javascript section, not in PHP.


Excellently working bro! Thanks alot! :bananaman:
Was This Post Helpful? 0
  • +
  • -

#7 Programmer2004   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 96
  • Joined: 25-October 17

Re: Disable Mouse Wheel Click in Chrome

Posted 16 September 2018 - 08:00 AM

That's great, you're welcome. :)
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,941
  • Joined: 08-August 08

Re: Disable Mouse Wheel Click in Chrome

Posted 16 September 2018 - 07:49 PM

Moving to jQuery.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1