What does javascript:void(0) mean?
You might have occasionally came across “javascript:void(0)” in an HTML Document. It is often used when inserting an expression in a web page might produce some unwanted effect. To remove this effect, “javascript:void(0)” is used. This expression returns undefined primitive value.
This is often used with hyperlinks.
Sometimes, you will decide to call some JavaScript from inside a link. Normally, when you click a link, the browser loads a brand new page or refreshes the same page (depending on the URL specified).
But you most likely don’t desire this to happen if you have hooked up some JavaScript thereto link.
To prevent the page from refreshing, you could use void(0).
Using “#” in anchor tag: When writing the following code in the editor, the web page is refreshed after the alert message is shown.
Example:
<!DOCTYPE html> <html> <head> <title>without JavaScript:void(0)</title> </head> <body> <center> <h1 style="color:green">GeksforGeeks</h1> <h3>without JavaScript:void(0)</h3> <a href="#" ondblclick="alert('Welcome to Geeks for Geeks')"> Double click on me </a> </center> </body> </html> |
Output:


Using “javascript:void(0);” in anchor tag: Writing “javascript:void(0);” in anchor tag can prevent the page to reload and JavaScript functions can be called on single or double clicks easily.
Example:
<!DOCTYPE html> <html> <head> <title>JavaScript:void(0)</title> </head> <body> <center> <h1 style="color:green">GeksforGeeks</h1> <h3>JavaScript:void(0)</h3> <a href="javascript:void(0);" ondblclick="alert('Welcome to Geeks for Geeks')"> Double click on me </a> </center> </body> </html> |
Output:


Recommended Posts:
- Maximum number of consecutive 1's in binary representation of all the array elements
- When to use Vanilla JavaScript vs jQuery ?
- How to detect the user browser ( Safari, Chrome, IE, Firefox and Opera ) using JavaScript ?
- Maximum frequency of a remainder modulo 2i
- How to animate scrollTop using jQuery ?
- How to select text nodes using jQuery ?
- Why require_once() function is so bad to use in PHP ?
- How to convert arguments object into an array in JavaScript ?
- HTML | DOM FocusEvent
- PHP | Imagick setCompression() Function
- PHP | Imagick getImageAlphaChannel() Function
- HTML5 | MathML <munderover> Tag
- PHP | image_type_to_extension() Function
- PHP | Imagick setFormat() Function
- PHP | Imagick getImage() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



