void operator evaluates the given expression and then returns a value of
You may ocassionally encounter an HTML document that uses
void is often used when, inserting an expression into a web page may produce an unwanted side-effect.
undefined primative value.
A common usage of
To prevent the page from refreshing, you could use
We have a link that should only do something (display a message) upon two clicks (a double click). If you click once, nothing should happen. We can specify the double click code by using the
ondblclick event handler. To prevent the page reloading upon a single click, we can use
Same Example, but without
Look at what would happen if we didn't use
Did you notice the page refreshed as soon you clicked the link (actually, it refreshed the
<iframe> content on this example — therefore, the above link probably disappeared when you clicked it). Even if you double clicked and triggered the
ondbclick event, it will still happen!
void operator can be useful when you need to call another function that may have otherwise resulted in an unwanted page refresh.
If the website uses a
<base> element, the link will lead to the URL specified in the
<base> element. Either way, using
void(0) will prevent this from happening.
The practice of using
<button> element for example). You can use CSS to change its appearance if it needs to look more like a hyperlink.
However, if you must use the
<a> element, try to ensure that the
When doing this, you can use
event.preventDefault() within an
onclick event handler to prevent the page from refreshing.
return false; will do the same thing.