-3

Anchor tag is opening in new window while we have clicked middle button of mouse. I want to disable this new window/tab. The belwo provided code is working in chrome.

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

Checked following links:

  1. Triggering onclick event using middle click
  2. Disable middle mouse button for modal dialog

According to some better is to change anchor tag to some other tabs, but I need the default behavior of anchor tab in left click, I need to disable middle and right clicks. Somebody help me to solve this.

Note: Many questions are asked regarding the same, but this exact issue is not yet asked.

This is not working in Firefox. I need to solve this issue in Firefox too.

fiddle

Community
  • 1
  • 1
Anjith K P
  • 2,158
  • 27
  • 35
  • You didn't say what's wrong with your solution. – Itay Aug 31 '13 at 06:31
  • 1
    From one of the answers in your link #2: The default function of middle mouse button can't be disabled in firefox. As stated here. Firefox and the other Gecko browsers have released control of the right mouse button, but the default action of a middle click can not be disabled. You can change what the default action is by editing the middlemouse settings on the "about:config" URL, but Javascript can't cancel them. – leftclickben Aug 31 '13 at 06:58
  • possible duplicate of [Triggering onclick event using middle click](http://stackoverflow.com/questions/1795734/triggering-onclick-event-using-middle-click) – putvande Aug 31 '13 at 10:52
  • @Itay this is not working in firefox – Anjith K P Sep 03 '13 at 08:49
  • @putvande, I have solved this issue in chrome. I need to find a solution in firefox for this. Please help – Anjith K P Sep 18 '13 at 12:57

2 Answers2

1

This is for blocking middle clicking entire document on firefox. You can check whatever element by e.target. Its need jQuery, but u could also use vanilla js

$(document).on('auxclick', function(e) {
    if( e.button == 1 ) 
    alert('Its blocked globally');
    e.preventDefault();
    return false;
  }
})
-3

Pure html solution, no javascript:

<a href="javascript:void(0)" onclick="window.location = 'http://www.stackoverflow.com';">Click me</a>
kcsoft
  • 2,917
  • 19
  • 14
  • Thnks for ur suggestion. In firefox this too is not working, open this in new tab "javascript:void(0)" – Anjith K P Sep 03 '13 at 08:56
  • 5
    javascript:void(0) and onclick="window.location = 'http://www.stackoverflow.com';" is javascript. You've just embedded it directly into your element rather than separate the logic – TommyBs Sep 03 '13 at 10:33