Table of contents
1.
Introduction
2.
What Does JavaScript Void 0 Mean?
3.
Using “#” in anchor tag
3.1.
HTML
4.
Using "javascript:void(0);" in Anchor Tag
4.1.
HTML
5.
Preventing a Page to Reload Using JavaScript Void 0
6.
JavaScript Void 0 Alternatives
6.1.
1. Using event.preventDefault()
6.2.
2. Using a Button Element
7.
Frequently Asked Questions 
7.1.
Why use "javascript:void(0);" instead of "#" in links?
7.2.
Can "javascript:void(0);" affect SEO?
7.3.
Is there a better alternative to "javascript:void(0);" for modern web development?
7.4.
What Does Form Action JavaScript Void 0 Mean?
8.
Conclusion
Last Updated: Sep 22, 2024
Easy

Javascript Void 0

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Initially, "javascript:void(0);" might look like a mysterious piece of code. But it's actually a simple yet powerful tool used in web development. This piece of JavaScript code is often found in the href attribute of anchor tags (<a>). It’s mainly used to prevent the default link behavior. That means when you click on a link with "javascript:void(0);", it won't take you to a new page or refresh the current one. Instead, it allows for other JavaScript functions to run without changing the page's URL. 

Javascript Void 0

In this article, we'll explore how "javascript:void(0);" is used, particularly in creating dynamic and interactive web elements. We'll also look at an alternative, using the “#” in anchor tags.

What Does JavaScript Void 0 Mean?

javascript:void(0) is a JavaScript expression used to create a link or an action that does not lead to any page navigation or reload. The void operator evaluates the expression provided and returns undefined. By using 0 (or any other value), you ensure that nothing is returned, effectively preventing any default action associated with the link.

When you see javascript:void(0) in an anchor (<a>) tag, it signifies that clicking the link will not change the current page or navigate away from it. This is particularly useful in scenarios where you want to handle events without triggering a page reload, such as when executing JavaScript functions, displaying modals, or implementing interactive features.

For example:

<a href="javascript:void(0);" onclick="alert('Hello!')">Click Me</a>


In this case, clicking the link will trigger an alert saying "Hello!" without navigating to a different page or refreshing the current one. This makes javascript:void(0) a handy tool for developers looking to enhance user interactions without the hassle of page transitions.

Using “#” in anchor tag

When you see a "#" in the href attribute of an anchor tag (<a href="#">), it's doing something pretty straightforward. This symbol is often used as a placeholder. It means that when you click on the link, instead of going to a different page, you stay right where you are. But, there's a little catch. Using "#" can still change your page a bit—it might scroll you back to the top. That's not always what you want, especially if you're trying to make a page with smooth navigation and user experience. That's where "javascript:void(0);" comes into play, as it prevents any change, including that unwanted jump to the top. But "#", despite its simplicity, has its own uses, especially if you're linking to a specific part of the same page, called an anchor point. It's all about choosing the right tool for the job.

Imagine you have a webpage with a lot of content, maybe an article or a blog post, and you want to create a link at the top that quickly takes readers to a comment section at the bottom. Here's how you could do it:

  • HTML

HTML

<!DOCTYPE html>

<html>

<head>

   <title>Simple Page</title>

</head>

<body>

<h2>Article Title</h2>

<p>This is a simple example article. It has several sections, but you want to jump directly to the comments section at the bottom.</p>

<!-- Link to jump to the comments section -->

<a href="#comments">Go to Comments</a>

<!-- Other content goes here -->

<p>More article content...</p>

<!-- Comments Section -->

<h3 id="comments">Comments</h3>

<p>This is where user comments would be displayed.</p>

</body>

</html>

Output

output

In this HTML code, the <a href="#comments">Go to Comments</a> line creates a link labeled "Go to Comments". When clicked, it looks for an element with the id "comments". Since the <h3> tag has id="comments", the browser scrolls the page to bring this heading into view.

This use of "#" in the anchor tag is pretty handy for in-page navigation. However, it's a different case from using "#" just by itself, which, as mentioned earlier, can lead to the page jumping to the top without linking to any specific section.

Using "javascript:void(0);" in Anchor Tag

When we use "javascript:void(0);" inside an anchor tag, it's like telling the link to do nothing by default. It's a way to make the link clickable without leading you anywhere or causing any page refresh. This is especially useful when you're working with JavaScript to add interactive features to your webpage.

Here's a simple example to illustrate this:

  • HTML

HTML

<!DOCTYPE html>

<html>

<head>

   <title>Interactive Button Example</title>

   <script type="text/javascript">

       function showMessage() {

           alert("Hello, world!");

       }

   </script>

</head>

<body>

<!-- Anchor tag with javascript:void(0); -->

<a href="javascript:void(0);" onclick="showMessage()">Click Me!</a>

</body>

</html>

Output

output

 

In this HTML code, we have an anchor tag that looks like a regular link saying "Click Me!". However, instead of taking you to another page, it runs a JavaScript function called showMessage() when clicked. This function then displays a simple message box saying "Hello, world!".

The "javascript:void(0);" part ensures that clicking the link doesn't change the URL or refresh the page—it just triggers the function. This technique is handy for making webpages more dynamic & interactive without interrupting the user's experience by reloading the page.

Preventing a Page to Reload Using JavaScript Void 0

In web development, you may want to prevent a page from reloading when a link is clicked. Using javascript:void(0) is a common technique to achieve this. When you use javascript:void(0) in an anchor (<a>) tag, it effectively tells the browser to do nothing when the link is clicked. This means that the default behavior of navigating to a new URL or reloading the page is suppressed.

For example, consider the following HTML:

<a href="javascript:void(0);" onclick="myFunction()">Click Me</a>


In this code, when the link is clicked, it will execute myFunction() without causing the page to reload. This is useful for scenarios where you want to handle events dynamically without refreshing the page.

JavaScript Void 0 Alternatives

While javascript:void(0) is widely used, there are several alternatives to prevent page reloads in JavaScript.

1. Using event.preventDefault()

You can use the event.preventDefault() method in an event handler to prevent the default action of an anchor tag. This is a cleaner approach compared to javascript:void(0).

Example:

<a href="https://example.com" id="myLink">Click Me</a>
<script>
document.getElementById('myLink').addEventListener('click', function(event) {
   event.preventDefault();
   myFunction();
});
</script>


In this example, clicking the link will not navigate to "https://example.com." Instead, it will call myFunction() without reloading the page.

2. Using a Button Element

If the action does not require navigation, consider using a <button> element instead of an anchor tag. Buttons do not have a default navigation behavior.

Example:

 

Here, clicking the button will call myFunction() without any risk of reloading the page.

<button onclick="myFunction()">Click Me</button>


Both methods provide cleaner and more semantic alternatives to javascript:void(0), improving code readability and maintainability.

Frequently Asked Questions 

Why use "javascript:void(0);" instead of "#" in links?

"javascript:void(0);" stops the link from doing anything, including not jumping to the top of the page like "#" does. It's useful when you want to run JavaScript without changing the page's look or position.

Can "javascript:void(0);" affect SEO?

Since "javascript:void(0);" doesn't lead to a new page or content, it generally doesn't affect SEO directly. However, ensure that important links for navigation or information are not solely reliant on JavaScript to be accessible.

Is there a better alternative to "javascript:void(0);" for modern web development?

Yes, using button elements or proper URL links with event listeners in JavaScript can be more semantic and accessible. "javascript:void(0);" is handy but consider newer, more accessible approaches for interactive elements.

What Does Form Action JavaScript Void 0 Mean?

Using javascript:void(0) as a form action prevents the form from submitting and reloading the page. It allows developers to handle form events with JavaScript while keeping the user on the same page.

Conclusion

In this article, we learned how to use "#" and "javascript:void(0);" in anchor tags. We started by understanding how "#" can serve as a placeholder or link to page sections but might cause unintended jumps to the top of the page. We then explored how "javascript:void(0);" provides a way to make clickable elements without changing the page's state, ideal for triggering JavaScript functions without affecting the page's URL or causing a refresh.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass