Last Updated : 12 Jul, 2025
javascript:void(0) is commonly used in HTML to create a link that doesn’t perform any action or navigate to a new page. When placed in the href attribute of an <a> tag, it allows the link to execute JavaScript code without reloading or changing the current page. This is particularly useful for triggering JavaScript functions while preventing the default behavior of navigating away from the current page.
Syntax
<a href="javascript:void(0)" onclick="JavaScriptFunction()">Click Here</a>
In the above syntax:
Now let's understand this with the help of example:
HTML
<html>
<body>
<a href="javascript:void(0)" onClick=
"document.getElementById('demo').style.display='block'">
Click to Show Message
</a>
<div id="demo" style="display:none;">
<p>This is a hidden message that appears
when you click the link!</p>
</div>
</body>
</html>
Output
What Does javascript:void(0) Mean?In this code
href="javascript:void(0)"
ensures that clicking the link doesn't navigate to a new page.Below are the components of javascript:void(0), which help to understand how it works:
Developers use javascript:void(0) for a few reasons:
While javascript:void(0) is commonly used, there are a few alternatives that can achieve the same results
1. Using "#" in an anchor tagIt is the another way to prevent navigation is by using # in the link’s href attribute. The hash sign # refers to the current page, and clicking it won’t cause any real navigation:
html
<html>
<body>
<a href="#" onClick="alert('Welcome to Geeks for Geeks')">
Click on me
</a>
</body>
</html>
2. Using event.preventDefault()
It is the more advanced method is to use JavaScript to cancel the default behavior of the link or button
HTML
<html>
<head>
<script>
function handleClick(event) {
event.preventDefault();
alert('Link clicked!');
}
</script>
</head>
<body>
<a href="https://www.geeksforgeeks.com/"
onclick="handleClick(event)">Click Me</a>
</body>
</html>
When to Avoid javascript:void(0)?In this approach, event.preventDefault() is used within the onclick event handler to prevent the default action (navigating to a new page) and perform custom JavaScript actions. This method is more useful because it doesn't modify the href attribute.
javascript:void(0) allows links to run JavaScript without navigating away from the page, making it useful for actions like showing popups. However, it can cause accessibility and SEO issues, so alternatives like # or event.preventDefault() may be better in some cases. Always use it carefully to ensure a smooth user experience.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4