javascript:void(0) Meaning
We often use code like javascript:void(0), so what does javascript:void(0) mean in JavaScript?
The most critical part of javascript:void(0) is the void keyword. void is a very important keyword in JavaScript; this operator specifies that an expression is to be evaluated but no value is returned.
The syntax format is as follows:
void func()
javascript:void func()
or
void(func())
javascript:void(func())
The following code creates a hyperlink, and nothing will happen when the user clicks it.
Example
<a href="javascript:void(0)">Clicking here does nothing
a
Try it »
When the user clicks the link, void(0) evaluates to 0, but has no effect in Javascript.
In the following example, an alert message is displayed after the user clicks the link:
Example
<
p
Click the following link to see the result:
p
<a href="javascript:void(alert('Warning!!!'))">Click me!
a
Try it »
In the following example, the parameter a will return undefined:
Example
function
getValue
(
)
{
var
a
,
b
,
c
;
a
=
void
(
b
=
5
,
c
=
7
)
;
document
.
write
(
'
a =
'
a
'
b =
'
b
'
c =
'
c
)
;
}
Try it »
The difference between href="#" and href="javascript:void(0)"
# contains a location message; the default anchor is **#top**, which is the top of the web page.
While javascript:void(0) simply represents a dead link.
When a page is very long, use # to locate a specific position on the page, in the format:# + id。
If you want to define a dead link, please use javascript:void(0).
Example
<a href=">javascript:void(0);
"
Click me, nothing happens!
a
<a href="#pos">Click me to jump to a specified position!
a
<
br
...
<br>
<p id="pos">Target anchor at the bottom
p
Try it »