Home >Web Front-end >JS Tutorial >Definition and detailed explanation of operator void(0) in Javascript
void in Javascript is an operator that specifies to calculate an expression but does not return a value.
The most critical thing in javascript:void(0) is the void keyword. void is a very important keyword in JavaScript. This operator specifies to calculate an expression but does not return a value.
void operator usage format is as follows:
1. javascript:void (expression) 2. javascript:void expression
expression is a Javascript standard expression to be evaluated. The parentheses outside the expression are optional, but are a good practice to write.
You can specify a hyperlink using the void operator. The expression will be evaluated but nothing will be loaded into 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 the link, the page will scroll up to the top of the page, #The default anchor point is #TOP (actual testing found that the scroll bar will scroll to the top ) The above four methods only represent a dead link and will not jump or return to the top.
Example - Why does location.href not jump automatically?
<a href="javascript:void(0)" onclick="delete('123')">删除</a> function delete(id){ if(confirm("确实要删除[为什么location.href不自动跳转?]吗?")) { location.href="/delete.jsp?id=" + id; } }
No matter how you check the above code, there is no problem, and location.href="/delete.jsp?id=" + id; works well in other places. Why does this code work?
The reason is that void(0) changed the code to:
< a href="javascript:delete('123')">删除</a>function delete(id) { if(confirm("确实要删除[为什么location.href不自动跳转?]吗?")) { location.href="/delete.jsp?id=" + id; } }
We found that the page jumped immediately and the corresponding data could be deleted normally. Why?
Because void is an operator, it will calculate an expression, but will not return a value. Of course, it will not change any content of the current page, and it will not jump normally.
Explanation
void OperatorEvaluates the expression and returns undefined. This operator is most useful when you want to evaluate an expression but don't want the result to be visible to the rest of the script.
Directly using javascript:void(0) for links (href) may cause some problems in IE, such as causing the gif animation to stop playing, etc. Therefore, the safest way is to use "#". To prevent jumping to the top of the page after clicking the link, the onclick event can return false.
【Related Recommendations】
1. Special Recommendation:"php Programmer Toolbox" V0.1 version Download
2. Free js online video tutorial
#3.php.cn Dugu Jiujian (3) - JavaScript video tutorial##4.
Solution to javascript:void(0) under IE6 Invalid methodsSummary of the difference between href=javascript:void(0) and href=Use with caution javascript:void(0), why is it not good to write like thisThe above is the detailed content of Definition and detailed explanation of operator void(0) in Javascript. For more information, please follow other related articles on the PHP Chinese website!