3 ms·
You need to make sure your click handlers detect meta keys: navToThing: (e) -> if not (e.ctrlKey or e.metaKey) e.preventDef
by seiji 14y ago
You need to make sure your click handlers detect meta keys:
navToThing: (e) ->
if not (e.ctrlKey or e.metaKey)
e.preventDefault()
@navigate e.currentTarget.pathname
If someone is holding down a meta key when they click a link, get the hell out of the way and just let the event happen. If they click without a modifier, feel free to prevent the event and continue updating the state of your fancy one-page app.
Sadly, most sites just copy/paste javascript from ten year old beginner PHP tutorials and will never detect meta keys to allow for open-in-new-tab.
- martin-adams 14y agoAs someone who hasn't explored this much, why is a meta key problematic?
- seiji 14y agoI think most people open-in-new-tab by command or control clicking on a link. The event handler can detect the key being held down while you click a link. We're basically guessing their intent and not running preventDefault if they are trying to open things in a new tab.
- mschuster91 14y agoExactly... and we're already "guessing intents" way too much! It's all Apple-style, leave the user with as few choices as possible.
- chc 14y agoAs seiji explained, modifier keys are used to indicate that you want to do special things with links such as open in a new tab or download the linked document. A naive click override will ignore the modifiers and treat all clicks the same even though the intent is different.