简体   繁体   English

捕获事件点击“在新标签中打开链接”javascript或jquery - 隐藏URL

[英]Capture the event click of “Open Link in New Tab” javascript or jquery - Cloaking URL

I know that I can capture the events of click: left, right and middle button by: 我知道我可以通过以下方式捕获点击事件:左,右和中间按钮:

contextmenu    
e.which

But, I need to capture the event click of "Open Link in New Tab": When you do right click in a link, it shows a menu with the option "Open Link in New Tab". 但是,我需要捕获事件点击“在新标签中打开链接”:当您右键单击链接时,它会显示一个菜单,其中包含“在新标签页中打开链接”选项。

How can I capture that event ? 我该如何捕获该事件?

PD: is not the right click event, is the event when you do click in "Open Link in New Tab". PD:不是右键单击事件,是您单击“在新标签中打开链接”时的事件。

The main reason is, I'm trying to cloak links for affiliated sites, but when I do right click and choose the option "Open Link in New Tab", not working, it doesn't show me the real hidden url. 主要原因是,我正在试图隐藏附属网站的链接,但是当我右键单击并选择“在新标签中打开链接”选项,不工作时,它不会向我显示真正的隐藏网址。

I found the script searching in google but I edited the script to my needs, but I have the problem that I told above. 我发现脚本在谷歌搜索,但我根据我的需要编辑了脚本,但我有上面提到的问题。

The code: 代码:

(function ($) {

    ninja_href(".ninja-href");

function ninja_href_call(e,which)
{

  var ninja_url = e.target.getAttribute('data-ninja-url');
  var ninja_target = e.target.getAttribute('data-ninja-target');

  if(ninja_target == null || typeof ninja_target == undefined || which === 3)
  {
    ninja_target = "_self";
  }

  if(which === 2)
  {
    ninja_target = "_blank";
  }


  var win = window.open(ninja_url, ninja_target);
  if (win && ninja_target == "_blank")
  {
    win.focus();
  }
}



function ninja_href(element)
{
  if(element == null || typeof element == undefined){
    element = ".ninja-href";
  }

  if (document.addEventListener) 
  {
    document.addEventListener('click', function(e) {
      if(e.target && e.target.matches(element))
      {
        if (e.which === 1 || e.which === 2) 
        {
          e.preventDefault();
          ninja_href_call(e,e.which);

        }
      }
    }, false);

    document.addEventListener('mousedown', function(e) {
        if(e.target && e.target.matches(element))
        {
            if (e.which === 2) 
            {
                e.preventDefault();
                ninja_href_call(e,e.which);

            }
        }

    }, false);

    document.addEventListener('contextmenu', function(e) {
        console.warn(e);
      if(e.target && e.target.matches(element))
      {

      }
    }, false);
  } else {
    document.attachEvent('click', function() {
      if(e.target && e.target.matches(element))
      {
        if (e.which === 1 || e.which === 2) 
        {
          e.preventDefault();
          ninja_href_call(e,e.which);

        }
      }
    });

  }

}

}(window.jQuery));

Any idea 任何想法

Please take a look below... This is how you can handle it. 请看下面......这是你如何处理它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM