Javascript Tutorial -- Doing...Nothing...with void()
One of the really cool things about Javascript is that you can update pieces of an HTML document without refreshing the whole page. Sometimes to do this you will need to reference a URL, but you don't want to actually load a new page. This is where the void(0) function can be useful.
But first, a little theory. Modern browsers like Firefox, Opera and Internet Explorer can reference a Javascript command or function rather than a web address by replacing the "http://" portion of the address with "javascript:" and by replacing the hostname with the Javascript command or function. For example:
<a href="javascript:alert('This is a valid URL')">
Click here for an example.
But there's a catch. Suppose you call a function that returns a value? In that case, your web browser will replace the content on the page with the return value of the function. Click here to test this for yourself.
To keep from replacing your content with the return value of a function, you need to pass that function (or command) as a parameter to the void() function. Because void() is a function, it will still process the function or command that you are calling as a parameter to the void() function, but because the void() function does not return a value itself, the web browser will not replace the content of your web page.
Here is a sample HTML document that uses the void() function for this purpose.