javascript: What does void of 0 mean and the difference between href= and href=javascriptvoid of 0

  • 2020-10-23 20:53:15
  • OfStack

In Javascript, void is an operator that specifies that an expression is to be evaluated but no value is returned.

The void operator usage format is as follows:

1. javascript:void (expression)

2. javascript:void expression

expression is a standard expression for Javascript to be evaluated. The parentheses around the expression are optional, but it is a good habit to write them.

You specify hyperlinks using the void operator. The expression is evaluated but nothing is loaded in the current document.

Example - Click a hyperlink without jumping

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 will roll up to the top of the page. The default # anchor point is #TOP (the actual test found that the scroll bar will roll to the top). The above four methods only mean that a dead link is a dead link and will not jump or return to the top.

Example - Why does ES58en. href not jump automatically?


<a href="javascript:void(0)" onclick="delete('123')"> delete </a>
function delete(id){
 if(confirm(" I do want to delete [ why location.href Does not jump automatically? ] ? ")) {
  location.href="/delete.jsp?id=" + id;
 }
}

location. href="/ delete. jsp? id=" + id; Why does this code work when it works everywhere else?

The reason is that void(0) changed the code to:


<a href="javascript:delete('123')"> delete </a>function delete(id) {
 if(confirm(" I do want to delete [ why location.href Does not jump automatically? ] ? ")) {
  location.href="/delete.jsp?id=" + id;
 }
}

We found that the page jumped immediately and deleted the corresponding data normally. Why? #63;

Because void is a 1 operator, it evaluates an expression but does not return a value, and of course does not change anything about the current page, so it does not jump normally.

instructions

The void operator evaluates the expression and returns undefined. This operator is most useful when you want to evaluate an expression, but you don't want the rest of the script to see the result.

Link (href) using javascript:void(0) in IE may cause 1 problem, such as: gif animation stopped playing, so, the safest way is to use "#". The onclick event return false is sufficient to prevent you from clicking a link and jumping to the top of the page.

PS: The difference between href=# and href=javascriptvoid(0)

# "contains 1 location information
The default anchor point is # top, which is the top of the page
javascript:void(0) simply means one dead link
That's why sometimes the page is long and the link is # but it jumps to the top of the page
But javascript: void (0)
That's not the case so it's better to use void(0) when you call the script
or < input onclick > < div onclick > Etc.

Several ways to open new window links

1.window.open('url')

2. Use custom functions


<script>   
function openWin(tag,obj)   
{    
obj.target="_blank";    
obj.href = "Web/Substation/Substation.aspx?stationno="+tag;    
obj.click();   
}   
</script> 
<a href="javascript:void(0)" onclick="openWin(3,this)">LINK_TEST</a> 
window.location.href="" 

-------------------------------------------------------------------------------

If it's a #, it jumps to the top.

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 >
5: < span style="cursor:hand" > < /span > (Does not appear in FF)

-------------------------------------------------------------------------------

javascript:void(0) and the difference between href=# and href=javascriptvoid(0).


Related articles: