1. Javascript intro
  2. Javascript basics
  3. Javascript variables
  4. Javascript functions
  5. Javascript popup boxes
  6. Javascript conditions
  7. Javascript loops
  8. Javascript arrays
  9. Javascript objects
  10. Javascript strings
  11. Javascript events
  12. Javascript errors
  13. Javascript DOM
  14. Javascript elements
  15. Javascript new windows
  16. Javascript date & time
  17. Javascript cookies
  18. Javascript print
  19. Javascript redirect
  20. Javascript void
  21. Javascript summary

Javascript void(0)

Usually, when the user clicks a link on a page a new page loads, but this is not always the desired course of action. For example, when the user clicks a link on a page, you might want to update the value of a field in a form or the value of a variable. To prevent a page from reloading when a link is clicked, the void(0) function is used.

<a href="" onclick="alert('You have clicked the button')>Click here!</a>

Click on the above link. Even though the code within it specifies to display a message in an alert box (and this action does take place), the page will reload automatically.

We can fix this with void(0):
<a href="javascript:void(0);" onclick="alert('You have clicked the button')>Click here!</a>

In the above example, a message is displayed in an alert box as intended and void(0) prevents the page from reloading.

© Copyright 2013-2014
Terms of use | Privacy policy | Copyright information