Cara menggunakan href=javascript:void(0 not working)

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: 

html




<h1 style="color:green">GeeksforGeeks</h1>

<h3h10h3>

<h14 h15=h17 h18=style0>

style2style3h14>

<h14 h15=h17 h18==3>

style2=6

=7h14>

<"color:green"1>

style2"color:green"4

style2"color:green"6

"color:green"7"color:green"8

style2>GeeksforGeeks</0

=7"color:green"1>

<>GeeksforGeeks</5 >GeeksforGeeks</6=>GeeksforGeeks</8>GeeksforGeeks</9>GeeksforGeeks</5>

Output: 

Cara menggunakan href=javascript:void(0 not working)

What does javascript:void(0) mean?

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.

As a frontend developer or even as a backend developer maybe you have found javascript: void(0); written as a value for a href attribute inside an anchor tag (<a>) and you wondered what that means.

Example:


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

In this article, we will try to provide an explanation for the above syntax in order for you to know when and why it's used. To do that we will have to take it element by element.

Javascript void keyword

As in other programming languages, void keyword from Javascript, is an operator that has the role to tell the program NOT to return the result of an expression and it returns undefined instead.

For example, the following expression will return the value of the mathematical operation:


let sum = 1 + 1;
console.log(sum); // 2

And the next expression will return undefined:


let sum = void(1 + 1);
console.log(sum); // undefined

Even though the variable


let sum = 1 + 1;
console.log(sum); // 2

0 is undefined, Javascript still executes the expression

let sum = 1 + 1;
console.log(sum); // 2

2:


let sum = void console.log(1 + 1); // 2
console.log(sum); // undefined

From the above, we can observe that the code that it is after the void operator will always be executed but the return of the expression will always be undefined. The role of


let sum = 1 + 1;
console.log(sum); // 2

5 inside the void operator is simply just the one of a placeholder.

Use let sum = 1 + 1; console.log(sum); // 2 6 as the value of href

The


let sum = 1 + 1;
console.log(sum); // 2

7 part is a possible value for the

let sum = 1 + 1;
console.log(sum); // 2

8 attribute. This value tells your browser that the next text is a Javascript code and it should be treated accordingly. There are also other possible values for the

let sum = 1 + 1;
console.log(sum); // 2

8 attribute like mailto:, file:, tel:, sms: etc.

Cara menggunakan href=javascript:void(0 not working)

What is let sum = 1 + 1; console.log(sum); // 2 6?

By using


let sum = void(1 + 1);
console.log(sum); // undefined

1, the <a> tag will not send you to other web address. It will also not refresh the page as links usually do when you don't specify a value for the href attribute.

If you are wondering why we don't just remove the


let sum = 1 + 1;
console.log(sum); // 2

8 attribute, the answer is that by removing the

let sum = 1 + 1;
console.log(sum); // 2

8 attribute we will also remove the link appearance of the <a> element. That means that the cursor will not change when hovering the <a> tag. The cursor will act like it is on normal text.

In other words, by adding


let sum = void(1 + 1);
console.log(sum); // undefined

1 as the value of href attribute we will create a link that does nothing.

The example link from the beginning of this article will run the


let sum = void(1 + 1);
console.log(sum); // undefined

8 function when the tag is clicked, but without refreshing the page or sending you to a different page.

You may be interested: 15 JavaScript Courses - Learn Web Development, HTML, and CSS

When to use let sum = 1 + 1; console.log(sum); // 2 6?

Thankfully, with all the updates made in Javascript, you will never have to use it as there are better alternatives to it.

In order to prevent the default behavior of a <a> tag, it's generally recommended to use the


let sum = void console.log(1 + 1); // 2
console.log(sum); // undefined

1 tag for buttons and the <a> tag for links. Also, if you need or want to change the cursor, it's recommended to use CSS instead of changing the tag to <a>.

In conclusion, we hope that now you have a better understanding of what


let sum = 1 + 1;
console.log(sum); // 2

6 is. Since it does nothing that you can't do with only HTML/CSS, the syntax presented at the beginning is not so common these days. Use this piece of information only to know what role it has when you see it somewhere, but keep your code clean and follow our advice on best practices.