Hacker News new | past | comments | ask | show | jobs | submit login

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.



Basically 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.


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>


these 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...




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: