Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Detect Mouse Click and Open Target in New Tab

Tags:

jquery

I'm currently designing a simple forum application. It's mostly powered by jQuery/AJAX and loads everything on the same page; however, I know that sometimes users want to open several topics at once to look at them in new tabs when browsing a forum.

My solution to this was to detect when the middle mouse button is clicked and the left mouse button is clicked, and doing different actions then. I want to load the target with AJAX in the window when the left-mouse button is clicked, otherwise load it in a new tab.

My only problem is I don't know of a way to open a target location in a new tab with jQuery. Obviously opening new tabs at will isn't allowed, but is there a way to assign this type of behavior to a user-generated action?

Thanks!

like image 897
Phillip Avatar asked Sep 26 '11 11:09

Phillip


People also ask

How do you verify if links on click are opening a new page?

Add target="_blank" to your anchor markup, so the default link behavior is opening a new tab.

How to use mousedown in jQuery?

jQuery mousedown() MethodThe mousedown event occurs when the left mouse button is pressed down over the selected element. The mousedown() method triggers the mousedown event, or attaches a function to run when a mousedown event occurs. Tip: This method is often used together with the mouseup() method.

How do I open a new tab window?

Open a new tab Windows & Linux: Ctrl + t. Mac: ⌘ + t.


3 Answers

Please take look on sample code. It may help

<script type='text/javascript'>
    jQuery(function($){
        $('a').mousedown(function(event) {
            switch (event.which) {
                case 1:
                    //alert('Left mouse button pressed');
                    $(this).attr('target','_self');
                    break;
                case 2:
                    //alert('Middle mouse button pressed');
                    $(this).attr('target','_blank');
                    break;
                case 3:
                    //alert('Right mouse button pressed');
                    $(this).attr('target','_blank');
                    break;
                default:
                    //alert('You have a strange mouse');
                    $(this).attr('target','_self"');
            }
        });
    });
</script>
like image 72
Nimit Dudani Avatar answered Oct 15 '22 17:10

Nimit Dudani


<a href="some url" target="_newtab">content of the anchor</a>

In javascript you can use

$('#element').mousedown(function(event) {
      if(event.which == 3) { // right click
          window.open('page.html','_newtab');
      }
})
like image 37
Kanishka Panamaldeniya Avatar answered Oct 15 '22 17:10

Kanishka Panamaldeniya


You need to also consider that ctrl-click and cmd-click are used to open new tabs (in windows/linux and mac respectively. Therefore, this would be a more complete solution:

jQuery.isClickEventRequestingNewTab = function(clickEvent) {
    return clickEvent.metaKey || clickEvent.ctrlKey || clickEvent.which === 2;
};
like image 7
Willster Avatar answered Oct 15 '22 16:10

Willster