javascript - .click() event when 'Open in new tabwindow' - Stack Overflow

When I use .click() on an <a> tag, the event only works when I click on the element.Otherwise,

When I use .click() on an <a> tag, the event only works when I click on the element. Otherwise, if the user does a Right Click > Open in new window or Open in new tab, it doesn't trigger the click() event.

So, my question is...how do I trigger the click() event when user does right click > open in new tab/window?

Here is the HTML:

<a href="url">Click Me</a>

Here is the Js:

$("a").click(function(){
  alert('You clicked me!');
});

When I use .click() on an <a> tag, the event only works when I click on the element. Otherwise, if the user does a Right Click > Open in new window or Open in new tab, it doesn't trigger the click() event.

So, my question is...how do I trigger the click() event when user does right click > open in new tab/window?

Here is the HTML:

<a href="url">Click Me</a>

Here is the Js:

$("a").click(function(){
  alert('You clicked me!');
});
Share Improve this question asked May 27, 2012 at 12:58 Control FreakControl Freak 13.2k30 gold badges99 silver badges150 bronze badges 4
  • ummm.. thats only a right click, not open in new window/tab. – Control Freak Commented May 27, 2012 at 13:00
  • 1 I don't think it is possible. The only way I see is to catch mouse right button click and trigger click event manually. – VisioN Commented May 27, 2012 at 13:00
  • I don't think it's possible. I think one of the reason is that you don't want the website to be able to track the external link you are visiting. – gigadot Commented May 27, 2012 at 13:04
  • ok well i think i guess i will just have to use that.. – Control Freak Commented May 27, 2012 at 13:10
Add a ment  | 

1 Answer 1

Reset to default 3

You can try this code, but remember that changing the UI is not a good ideia:

var addEvent = (document.addEventListener) ?
    function(target,event,fn){
        if(target) return target.addEventListener(event,fn,false);
    }:
    function(target,event,fn){
        if(target) return target.attachEvent(('on' + event),fn);
    },
allLinks = document.links || document.getElementsByTagName('a');
for(var i=0;i<allLinks.length;i++)
    addEvent(allLinks[i],'mouseup',function(e){
        var e = e  || event;
        if(e.which===3){
            alert('Open in new tab/window');
            e.preventDefault();
            return false;
        }
    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744180537a4561960.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信