A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/what-does-javascriptvoid0-mean/ below:

What Does javascript:void(0) Mean? - GeeksforGeeks

What Does javascript:void(0) Mean?

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

Components of javascript:void(0)

Below are the components of javascript:void(0), which help to understand how it works:

Why Do Developers Use javascript:void(0)?

Developers use javascript:void(0) for a few reasons:

Alternative to javascript:void(0)

While javascript:void(0) is commonly used, there are a few alternatives that can achieve the same results

1. Using "#" in an anchor tag

It 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>

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.

When to Avoid javascript:void(0)? Conclusion

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