2

The following code works well in Chrome/Opera but not working in Firefox/IE.Please help me.

$('.class name of anchor tag').mousedown(function(e){
  if(e.which==2)
  {
     e.preventDefault();
     window.location = '';
  }
});        

I want to disable mouse middle button on hyperlink to prevent opening page in new tab.The code should work in FF/IE.I dont have any problem with capturing events. Event is triggering well.What is the code to prevent opening link once an user clicks middle button(ie when event triggerd).

SwR
  • 612
  • 1
  • 8
  • 21
  • try with === intead of ==. – ElTête May 10 '16 at 10:25
  • Already it satisfies if condition."==" working. – SwR May 10 '16 at 10:28
  • 1
    Here is an answer to your question http://stackoverflow.com/a/20155501/5882767 – zerohero May 10 '16 at 10:28
  • event is already triggered.There no problem.e.preventDefault and window.location is actually not working. – SwR May 10 '16 at 10:32
  • tangentially related: http://stackoverflow.com/questions/5392442/detect-middle-button-click-scroll-button-with-jquery – chiliNUT May 10 '16 at 10:43
  • Here,for me event is capturing.But my problem is I dont want to open the link in new tab. – SwR May 10 '16 at 10:50
  • Define "not working" – Lightness Races in Orbit May 10 '16 at 10:55
  • 1
    @LightnessRacesinOrbit The OP couldn't state his question correctly but I still think his question isn't answered. The middle click event fires a javascript event and it has a default behavior. The event is succesfully called in all browsers but even calling `preventDefault` on the event doesn't prevent it in some browsers. – Gokhan Kurt May 10 '16 at 11:11
  • You know, I really dislike the page you're creating. – SF. May 10 '16 at 11:50
  • @SF.What?I didnt get your comment. – SwR May 10 '16 at 11:55
  • 2
    I don't know the specific situation you're going to use this in, but in general, this seems like really poor design. If it's a link, it should act like a link, including default middle mouse click behavior. If it's not, don't use the tag. I've seen pages do this before and like @SF, I really didn't like that. – Kruga May 10 '16 at 12:37
  • 1
    @SwR: Essentially, you're crippling a good, native functionality of a browser. Usually, if I want to reference the prior page while going to a new one, I open the new one in a new tab through middle click. BTW: Did you account for rightclick-open in new tab? If you did, did you account for the RightToClick extension that deals with that? Did you account for "drag with right mouse button up above the link" Mouse Gesture that opens a link in a new tab, from the MozGest extension? Tabbing to the link and pressing the Menu button on keyboad? You're fighting an uphill battle against a windmill. – SF. May 10 '16 at 13:18
  • @SF. You are assuming things. For example I design a single page applicaton which uses middle mouse button gestures. But the default behavior for a middle click starts a page scrolling. If someday, a new version of a browser does not let me prevent that default, will you assume that I am trying to prevent the user from scrolling the page? – Gokhan Kurt May 10 '16 at 19:41

5 Answers5

1

Try with this

$('.class name of anchor tag').on("mousedown", "selector", function (e) {
GRESPL Nagpur
  • 2,048
  • 3
  • 20
  • 40
1

You can place the anchor tag element inside another element -which will act as "container"; -or (and) place another transparent element over it and handle/capture the click event through the parent/overlay element.

Update:

Even though the above-mentioned technique is a work-around, it could save time, and if an "appropriate fix" is unavailable, it could be the only option. This technique could also be useful elsewhere, where more control is required for click event handling.

If you do apply this technique, using an "overlay" you can detect:

if the middle-button was clicked -> ignore; else -> get the anchor tag's href value and navigate accordingly with something like this:

overlay.onclick = function(event)
{
    event = (event || window.event);

    if (event.which == 2 || event.button == 4 )
    { return; }

    window.location.href = this.parentNode.getElementsByTagName('a')[0].href;
};
  • there is a reason why it happens and creating a work-around like you suggested won't solve or address the actual issue – zerohero May 10 '16 at 10:28
  • 1
    @zerohero :: hey it's IE :P, and the version is not specified, at least I provided a technique that will definitely work.. kindly reverse the down-vote? .. I mean.. there's not much to say, either that or I have to cater for every **unspecified** issue that _could_ be the cause –  May 10 '16 at 10:38
  • @SwR :: thanks, that was useful, (about IE 11) and I up-voted your comment accordingly. If you find my answer at least useful, kindly up-vote? - _thanks_ –  May 10 '16 at 10:44
  • 1
    I want to disable mouse middle button on hyperlink to prevent opening page in new tab.The code should work in FF/IE.I dont have any problem with capturing events. Event is triggering well.What is the code to prevent opening link once an user clicks middle button(ie when event triggerd). – SwR May 10 '16 at 11:04
  • @SwR :: sorry, I've updated the code in the question accordingly, it should now ignore middle-mouse-click. –  May 10 '16 at 11:45
  • 1
    Just because he doesn't state an IE version doesn't mean that IE from version 9 is different to that of 10,11 etc. That's what makes it IE, they never fix their crap ;) – zerohero May 10 '16 at 12:40
0
$(document).on("mousedown", "selector", function (ev) {
    if (ev.which == 2) {
        ev.preventDefault();
        alert("middle button");
        return false;
    }
});
Mir
  • 126
  • 5
0

I solved my problem using the following code,though its not satisfactory,its working better than previous.

 $('.accordion-toggle').mousedown(function(e){
    if(e.which===2) {
        if(/Firefox|MSIE |Trident/i.test(navigator.userAgent)) {
            e.stopPropagation();
            e.preventDefault();
            window.location = 'index';
            return false;
        }
        else
        {
            e.preventDefault();
            window.location = '';
        }
    }
});

If anybody know,perfect solution,please respond.

SwR
  • 612
  • 1
  • 8
  • 21
0

Don't use an a-tag; use a span instead. No need to break the web.

If the question is: "How do I prevent things from working as intended?" the answer is usually "Don't do that."

If you don't control the source, change it from an a tag to a span: https://stackoverflow.com/a/13389835/474374

Community
  • 1
  • 1
Odalrick
  • 645
  • 5
  • 16