views:

28

answers:

2

Apart from the obvious onclick event handler, how can I detect if the user tabbed the link and hit enter or whatever other key/device that would make the link open?

Also, is it possible to detect a middle mouse button click (defaults to open in new tab)?

+1  A: 

Pressing enter will produce an onclick event. You do not need any special code to handle the enter key

Edit: As for middle click you can look here: http://www.quirksmode.org/js/events_properties.html It is not so simple (thanks to IE)

Edit: If you use jQuery it is very simple: http://stackoverflow.com/questions/1206203/how-to-distinguish-left-mouse-click-and-right-with-jquery/2725963#2725963

cjavapro
See my two edits above
cjavapro
+1  A: 

Assuming you want a separate solution to catching specifically an ENTER key press on a link, you might be able to use onkeyup and check if the event.keyCode equals 13 (ENTER key).

mrjames