最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to trap "open in a new tab" clicks in jquery.click - Stack Overflow

programmeradmin9浏览0评论

I have a jquery script that attaches a click event to every link, running an action when the link is clicked. This has been working great, but I just got some betatester feedback that's foiling me.

The user was right-clicking on the link and opening it in a new tab. When she did this, jquery didn't trap the click. BAD USER. I reproduced this with cmd-click as well.

Is there a way to trap these gestures, or this an inherent limitation?

I have a jquery script that attaches a click event to every link, running an action when the link is clicked. This has been working great, but I just got some betatester feedback that's foiling me.

The user was right-clicking on the link and opening it in a new tab. When she did this, jquery didn't trap the click. BAD USER. I reproduced this with cmd-click as well.

Is there a way to trap these gestures, or this an inherent limitation?

Share Improve this question edited Jan 4, 2020 at 18:58 monkey 5438 silver badges22 bronze badges asked Jun 15, 2009 at 10:55 Jason ButlerJason Butler 5,6494 gold badges24 silver badges18 bronze badges 1
  • I very much doubt this is possible given the DOM events you rely on pre-date tabs in browsers anyway. I don't know how you'd distinguish between opening in a new window. – annakata Commented Jun 15, 2009 at 11:10
Add a ment  | 

3 Answers 3

Reset to default 2

So you want to capture every click? Event the right or middle one? Shouldn't the mousedown event do just that?

Of course, she could right click a link just to "Copy Link Location"...

See if you can somehow make use of jQuery rightclick plugin:

http://abeautifulsite/notebook/68

Usage:

$(document).ready( function() {

    // Capture right click
    $("#selector").rightClick( function(e) {
        // Do something
    });

    // Capture right mouse down
    $("#selector").rightMouseDown( function(e) {
        // Do something
    });

    // Capture right mouseup
    $("#selector").rightMouseUp( function(e) {
        // Do something
    });

    // Disable context menu on an element
    $("#selector").noContext();

});

As for the cmd-clickie bit, I'm really not sure. In case it's helpful, here's the jQuery hotkeys plugin:

http://www.webappers./2008/07/31/bind-a-hot-key-bination-with-jquery-hotkeys/

I've seen jquery.rightclick.js code in firebug. There are modifiers with the mousedown and mouseup event like:

altKey ctrlKey

so you can use these two modifiers:

if(evt.altKey || evt.ctrKey)

in jquery.rightclick.js

发布评论

评论列表(0)

  1. 暂无评论