1
$(document).on('mousedown', 'a', function(event){ 
    event.preventDefault();

    if(event.which == 1){
        if($(this).attr('target') != '_blank'){
            loadpage($(this).attr('href'));
        }
    }
}).on('contextmenu', 'a', function(event){
    event.preventDefault();
});

Hello once again Stackoverflow!

For my current project I want to disable the right and middle mouse button on every link. And when clicked on with the left mouse button, if the link doesn't contain target="_blank", I need to call a function that loads that page using AJAX. (function loadpage()).

This piece of code works decently, although the middle mouse button still opens a new tab. How do I solve this?

Thanks in advance!

Thew
  • 15,789
  • 18
  • 59
  • 100
  • possible duplicate of [How to disable right-click context-menu in javascript](http://stackoverflow.com/questions/381795/how-to-disable-right-click-context-menu-in-javascript) – PlantTheIdea Nov 14 '13 at 18:24
  • @PlantTheIdea I have read that topic. Those answers did not help my problem. – Thew Nov 14 '13 at 18:25
  • 1
    This was cool in the nineties, today it's just annoying and great evidence of a retarded developer ! – adeneo Nov 14 '13 at 18:26
  • @Thew - alright, i'll jump to the point ... don't disable the right / middle buttons. certain browsers (Firefox) don't even allow you to prevent action (it still fires no matter how much magic you pull) because doing so is considered **bad practice**. to do so annoys the hell out of users and developers alike. – PlantTheIdea Nov 14 '13 at 18:29
  • This is to prevent multiple instances from running as its more like an app then a website. – Thew Nov 14 '13 at 18:31
  • If your app can't run multiple instances, the least of your troubles is annoying UX. – adeneo Nov 14 '13 at 18:32
  • This app will make itself clear it's not possible to run multiple instances, therefor removing the "annoying UX". Opening a second instance is not possible already as it displays a warning. Now to prevent users from trying something that can't be done already I want to disable opening a new tab. Note that this function only applies to links. In my opinion, it's kinda strange that people just come here to say that it's going to annoy my UX and leave without me telling the whole context. I just wanted an answer for my question, imo a context shouldn't have been given. – Thew Nov 14 '13 at 18:40

1 Answers1

2

Within that event handler, call

e.preventDefault():

$("#foo").on('click', function(e) { 
   if( e.which == 2 ) {
      e.preventDefault();
   }
});

or: Disable mouse wheel event by using JAVASCRIPT :

In IE:

document.attachEvent('onmousewheel', function(e){
     if (!e) var e = window.event;
     e.returnValue = false;
     e.cancelBubble = true;
     return false;
}, false);
In Safari:

document.addEventListener('mousewheel', function(e){
    e.stopPropagation();
    e.preventDefault();
    e.cancelBubble = false;
    return false;
}, false);
In Opera:

document.attachEvent('mousewheel', function(e){
    if (!e) var e = window.event;
    e.returnValue = false;
    e.cancelBubble = true;
    return false;
}, false);
In Firefox:

document.addEventListener('DOMMouseScroll', function(e){
    e.stopPropagation();
    e.preventDefault();
    e.cancelBubble = false;
    return false;
}, false);
r.r
  • 7,023
  • 28
  • 87
  • 129
  • 1
    Adding an `if` statement with `e.which == '2'` did not solve the problem in Google Chrome and Firefox. – Thew Nov 14 '13 at 18:26