3 ms·
How did the void(0) thing work in links? I remember being frustrated by it when I couldn't see where the link would take me, but never understood what was going
by tgb 9y ago
How did the void(0) thing work in links? I remember being frustrated by it when I couldn't see where the link would take me, but never understood what was going on.
- k__ 9y agothese links have an onclick handler. If the handler goes missing or fails, this construct will prevent the link from doing what it usually would. But using a button is better in such cases...
- et-al 9y agohttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... Back then, you'd use it with the `href` attribute to prevent the link from going anywhere (it would return `undefined`), then call a function with the `onclick` attribute. E.g. <a href="javascript:void(0)" onclick="clickHandler()">Click me</a>
- ChrisSD 9y agoBasically it defers to a javascript function instead of using the web's normal way of linking. An example of such a link would be: <a href="javascript:void(0)" onclick="dostuff()">Click me</a> href="javascript:void(0)" basically tells the browser to do nothing when a link is clicked. It's like returning false from an event listener. onclick="dostuff()" will then listen for clicks and call the `dostuff` function to update the page or load a new one. Nowadays it's considered bad practice and to be honest it wasn't exactly good practice in those days but it was very commonly used.