Skip to content
the poor coder

What does "javascript:void(0)" mean?

Poor Man
Published
Reading time
1 min read
On this page

What does "javascript:void(0)" mean?#

As a web developer, I have come across the term "javascript:void(0)" in my code, and it can be confusing to understand its purpose.

Essentially, "javascript:void(0)" is a JavaScript code that is used to prevent the default action of an HTML link from executing. When you click on a link, by default, the browser navigates to the URL specified in the link's href attribute. However, sometimes, you want to perform some JavaScript code instead of navigating to a new page.

In such cases, you can use "javascript:void(0)" as the value of the href attribute of the link. This tells the browser to execute the JavaScript code specified in an onclick event or in the link's href attribute itself. It effectively prevents the browser from navigating to a new page.

Example Usage:#

JavaScript

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

In the above example, when you click on the link, instead of navigating to a new page, it will execute the JavaScript code specified in the onclick attribute, which will show an alert box with the text "Hello World!"

Another common use case for "javascript:void(0)" is when you want to create a link that performs some JavaScript code and then redirects the user to a new page. In such cases, you can use "javascript:void(0)" as the value of the href attribute and then use window.location.href to redirect the user after performing some JavaScript code.

Example Usage:#

JavaScript

      <a href="javascript:void(0)" onclick="doSomething(); window.location.href='http://example.com';">Click Me</a>
    

In the above example, when you click on the link, it will execute the doSomething() function and then redirect the user to http://example.com.

Overall, "javascript:void(0)" is a useful code snippet that can help you manipulate the default behavior of links in HTML pages.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC