5 ms·
Ask HN: Please stop hijacking onclick on HTML links
I do not know why it is "en vogue" now to hijack the "onclick" event on links. From Google (which changes the href-attribute in search results to their redirector - yay for loading times on mobile) to Twitter (adding their own URL shortener on already shortened links) to news and porn sites preventing the middle click from opening in a new tab!
Please, stop messing with the way I browse the web, be it for marketing or whatever other reason.
- seiji 14y agoYou 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.
- J_Darnley 14y agoCongratulations, you have found one of the things I would remove from the javascript features of a browser if I had the skills to do so.