Javascript:void (0)
In JavaScript, Void is an operator that specifies that an expression is evaluated but does not return a value.
The void operator uses the following format:
1. javascript:void (expression)
2. Javascript:void expression
Expression is a standard of Javascript to evaluate. Parentheses on the outside of the expression are optional, but writing is a good habit.
You use the void operator to specify a hyperlink. The expression is evaluated but does not load any content at the current document.
Example-clicking a hyperlink does not jump
1:<a href= "# # # ></a>
2:<a href= "javascript:void (0)" ></a>
3:<a href= "javascript:void (null)" ></a>
4:<a href= "#" onclick= "return false" ></a>
After clicking on the link, the page rolls up to the top of the page, # The default anchor point is #TOP (the actual test finds that the scrollbar rolls to the top) and the above four methods simply indicate that a dead link does not jump or return to the top.
Example-why does Location.href not jump automatically?
<a href= "javascript:void (0)" onclick= "Delete (' 123 ')" > Delete </a>
function Delete (ID) {
if (Confirm ("Do you want to delete [why Location.href does not jump automatically?") ]? ")) {
Location.href= "/delete.jsp?id=" + ID;
}
}
The above code no matter how to check no problem, and location.href= "/delete.jsp?id=" + ID; it works elsewhere, why is this code OK?
The reason is that void (0) changed the code to:
<a href= "Javascript:delete (' 123 ')" > Delete </a>function Delete (id) {
if (Confirm ("Do you want to delete [why Location.href does not jump automatically?") ]? ")) {
Location.href= "/delete.jsp?id=" + ID;
}
}
We found that the page immediately jumps, can delete the corresponding data. Why?
Because void is an operator, an expression is evaluated, but no value is returned, and of course it does not change anything on the current page and will not jump normally.
Description
The void operator evaluates an expression and returns undefined. The operator is most useful when you want the value of an expression, but you do not want the remainder of the script to see the result.
Link (href) directly using javascript:void (0) in IE may cause some problems, such as: Cause GIF animation to stop playing, so the safest way is to use "#". To prevent clicking on a link and jumping to the top of the page, the OnClick event will return false.
Javascript:void (0)