Void 0 javascript href là gì năm 2024

You may have come across a website page where the href value of an <a> tag has a value of javascript:void(0). What does this mean?

The Solution

A javascript: URL prefix indicates to the browser that the JavaScript code following the prefix should be executed, instead of navigating to the URL. The void operator evaluates an expression and returns undefined. By running void(0) in the URL JavaScript code, nothing is evaluated or returned.

A link that does not navigate to a new page or section of a page may be used to define empty links during development, but using an href value of ”#” is a better option due to potential XSS vulnerabilities with the use of javascript: URL prefixes. This is further explained in the following article: Which href value should I use for empty JavaScript links, ”#” or “javascript:void(0)“?.

The void operator is often used merely to obtain the undefined primitive value, usually using “void(0)” (which is equivalent to “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).

"The reason you’d want to do this with the href of a link is that normally, a javascript: URL will redirect the browser to a plain text version of the result of evaluating that JavaScript. But if the result is undefined, then the browser stays on the same page. void(0) is just the smallest script possible that evaluates as undefined."

The return false:

Acts like a event.preventDefault negating it.

If you call a function like:

<button type="submit" onclick="return some_function();"></button>

And the some_function has a return false; the submit will not happen if you call it.. But a return true will continue with the submit when it's called.

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 >

< h3 h1`0`h3 >

< h1`4 `h1`5= `h1`7 `h1`8= `style`0> `

style`2`style`3`h1`4> `

< h1`4 `h1`5= `h1`7 `h1`8= =3> `

style`2=`6

=`7`h1`4> `

< "color:green"`1> `

style`2"color:green"`4

style`2"color:green"`6

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

style`2>GeeksforGeeks</`0

=`7"color:green"1> `

< >GeeksforGeeks</`5 >GeeksforGeeks</6= >GeeksforGeeks</8>GeeksforGeeks</9>GeeksforGeeks</`5`h1`1

Output:

Void 0 javascript href là gì năm 2024

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.

Example:

html

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

< h3 > `2`h3 >

< h1`4 `h1`5= > 9 `h1`8= <2> `

style`2`style`3`h1`4> `

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

=`7>GeeksforGeeks</5> `

< "color:green"`1> `

`style`2`h1`01

`"color:green"`7`h1`03

style`2>GeeksforGeeks</`0

=`7"color:green"`1`h1`1

Output:

Void 0 javascript href là gì năm 2024

What does javascript:void(0) mean?

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Void là gì trong JavaScript?

Void là gì? void là một từ khóa quan trọng trong JavaScript mà có thể được sử dụng như là một toán tử một ngôi xuất hiện trước toán hạng đơn của nó, mà có thể trong bất kỳ kiểu nào.