javascript - Prevent middle mouse click in a href with jQuery -


this may sound dumb question hear me out.

i'm trying prevent link being clicked middle-mouse button. ux reason i'm doing this, because anchor loads ajax content, , user don't know until clicks it.

here's problem:

/** *   mousedown or mouseup */ $(document).on("mousedown", function(e) {     if($(e.target).is("a.load-some-ajax-stuff") && e.which === 2) {         alert('foo'); // works if there's alert         e.preventdefault();     } });  /** *   click */ $(document).on("click", function(e) {     if($(e.target).is("a.load-some-ajax-stuff") && e.which === 2) {         e.preventdefault(); // should work, doesn't catch middle-mouse clicks     } }); 

snippet

$(document).on('mousedown', "a.load-some-ajax-stuff", function(e) {   	if( (e.which == 2) ) {              // alert('foo');  	    e.preventdefault();           }  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <a href="#" class="load-some-ajax-stuff">i won't open new tab when middle click me</a>

edit

it seems snippet above works in safari, not chrome.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -