0

Chrome :

Following code is working in Chrome.

 $('.links').click(function(e) {
    if(e.which == 2) {
       console.log(e.which);    // prints 2
       //e.preventDefault();
       //e.stopPropagation();
       return false;
    }
 });

Firefox :

Since above code doesn't catch middle button / mouse wheel click event in firefox, I tried following which is able to catch mouse wheel click event.

$('.links').mousedown(function(e) {
    if(e.which == 2) {
       console.log(e.which);     // prints 2
       //e.preventDefault();
       //e.stopPropagation();
       return false;
    }
 });

Above code prints 2. But return false; is not working.

When I replaced console.log with alert then it works. But I can't & don't want to use alerts.

I tried mouseup, mousewheel events also. But it didn't work.

I tried attachEvent also but, I got an error(attchEvent is not a function).

I am using below mentioned js files :

  1. jQuery-1.10.2.min.js
  2. jquery.easyui.min.js
  3. jquery-ui.js
  4. jquery.ui.core.js

You can refer below links for more clarity.

jsfiddle.net/nilamnaik1989/vntLyvd2/3

jsfiddle.net/nilamnaik1989/2Lq6mLdp

http://jsfiddle.net/nilamnaik1989/powjm7qf/

http://jsfiddle.net/nilamnaik1989/q6kLvL1p/

Following are some good links. But anyhow it doesn't solve my problem.

event.preventDefault() vs. return false

event.preventDefault() vs. return false (no jQuery)

http://www.markupjavascript.com/2013/10/event-bubbling-how-to-prevent-it.html

I need your valuable inputs.

Community
  • 1
  • 1
Nilam Patil - Naik
  • 75
  • 1
  • 2
  • 15
  • Works for me in Chrome && FF: http://jsfiddle.net/vntLyvd2/1/ – geedubb Jan 02 '15 at 11:45
  • @geedubb OP stated that alerts work, so your demonstration is pointless. – theonlygusti Jan 02 '15 at 11:46
  • @theonlygusti no you are wrong. Here is it with console.log http://jsfiddle.net/vntLyvd2/2/ Doesn't make any difference. FF & chrome both support console. IE doesn't – geedubb Jan 02 '15 at 11:50
  • Why do you think that it doesn't work to stop the click? When I try it, the default action is suppressed. – Guffa Jan 02 '15 at 11:55
  • @geedubb But that's not the problem, the problem is preventing the default. Seems like someone didn't read the OP *at all.* I'd recommend deleting your comments. – theonlygusti Jan 02 '15 at 11:56
  • 1
    @geedubb Thanks for reply. Please check out these two links. http://jsfiddle.net/nilamnaik1989/vntLyvd2/3/ http://jsfiddle.net/nilamnaik1989/2Lq6mLdp/ – Nilam Patil - Naik Jan 02 '15 at 12:17
  • @theonlygusti Yes you are right. I am not saying that console is creating problem. My concern is that if I put alert then it works, if I remove alert then it doesn't work. Please check out links which I shared in my previous comment. Thanks for your reply. – Nilam Patil - Naik Jan 02 '15 at 12:24
  • @NilamNaik apologies I misunderstood your problem to an extent. Seems that the built in functionality in Firefox to open a new tab with middle button is causing the issue. I have tried disabling event propagation on all anchors and still can't seem to prevent this. Would be interesting to see if it is possible to override this functionality. – geedubb Jan 02 '15 at 12:33
  • @geedubb No need apologies. Let's see what we can do or if someone help us. – Nilam Patil - Naik Jan 02 '15 at 12:47

3 Answers3

0

All click default actions should be cancelable. That's one of the points of this important event. However, certain browsers have exceptions:

  • IE 5-8 won't prevent the default on text inputs and textareas.
  • IE9/10 & Opera incorrectly un-check radio buttons when you click on another radio in the same group. It correctly doesn't check the new radio.
  • IE 5-8, Firefox, & Opera won't prevent the default on select boxes.
  • Firefox & Chrome feel that one radio button must be checked. If all are unchecked they’ll check the first one you click on, even if the default is being prevented.

See Events - click, mousedown, mouseup, dblclick for some more information.

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0

I had the same issue with firefox, related with

preventDefault();

Everything was working well in Safari, Chrome, Opera and even in IE9 (not kidding) But, after a lot of reading, I saw that the site was using and old jquery version (1.10), then updated to the latest one (2.1.4) the action was canceled even in Firefox.

Another thing to consider is that I used a variable named "keyPressed" like:

var keyPressed = event.keyCode || event.which || event.charCode

So it was easy for each browser to recognize the key event.

Hope this help!

vulcanR
  • 53
  • 1
  • 3
0

I have faced the similar problem in FF on middle click.

The following script fixed me the issue and it works fine in FF as well.

$(document).on('click', $(".content"), function(e) {
    if(e.button==1) {
        e.preventDefault();
        return false;
    }
})
Gavis
  • 117
  • 11