<a href="http://www.google.com" id="testlink">CatchMe</a> $('#testlink').attr("rel", $('#testlink').attr("href")); $('#testlink').attr("href",""); $('#testlink').keydown(function(event) { if (event.keyCode == '13') { alert("Don't you dare!"); } if (event.keyCode == '71') { location.href=$('#testlink').attr("rel"); } });
This code removes href (saves it to rel). Now you can catch keydowns. You can answer the enter key (13) as you like (in this case a warning). Subsequently, you can let the browser follow the link if you want. However, in this example, I allow the browser to follow the link when the g key is pressed (code 71).
Note that this also works when the href value is similar to "javascript: alert (" blah ").
Edit: this is much simpler (inspired by the answer to this question):
$('#testlink').click(function () {alert("hi"); return false; });
(return true if you want the link to follow the warning)
source share