开发者

jQuery: Detect Mouse Click and Open Target in New Tab

开发者 https://www.devze.com 2023-04-07 07:49 出处:网络
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

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!


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>


<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');
      }
})


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;
};


    $('#element').mousedown(function(event) {
          if(event.which == 3) { // right click
              window.open("newlocation.html","");
          }
    });

See it live http://jsfiddle.net/8CHTm/1/


The default action of the middle mouse button is to open in a new tab.

You could set the hyperlinks target attribute to _blank to open a new tab.

<a href="#link" target="_blank">Link</a>

You can also refer to this question How to distinguish between left and right mouse click with jQuery


Please try "_newTab" instead of "_blank"

window.open( URL , "_newtab");

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号