samuk
samuk

Reputation: 167

NULLify CTRL press in link clicks

I have a requirement in preventing the links in my applications from spilling over to new window/tab through CTRL keys. I have the following in my page now:

        $(document).click(function(e)
        {
            if (e.ctrlKey)
            {
                return (false);
            }
        });

This seems to be working fine for DIV clicks however link clicks are totally ignored. I am looking to see if I can NULLIfy the CTRL keypress alone and make it appear like simple click. Is that possible? A kind of forcing the event keyCode for ctrl alone to be zero?

Upvotes: 1

Views: 125

Answers (2)

Sameera Thilakasiri
Sameera Thilakasiri

Reputation: 9508

By "document", plus elements that may later be added via AJAX, using this simple one-liner:- Copy code

$(document).on("click", "*", function(e)  { 
    if (e.ctrlKey) return false; 
});

If you're version of jQuery is lower than v1.7 then use .live() instead of .on() (just change the word from "on" to "live").

Upvotes: 1

techfoobar
techfoobar

Reputation: 66693

You can use:

$('a').click(function(e) {
    if(e.ctrlKey) return false;
});

Check this demo: http://jsfiddle.net/gTG6Q/

Note: The user can still right click and open your link in a new tab. For preventing that, you will need to negate the contextmenu event as well.

Upvotes: 2

Related Questions