Analysis of the difference between a tag href=# and href=javascript:void(0) in html

黄舟
Release: 2017-07-27 13:36:01
Original
2137 people have browsed it

Let’s briefly talk about the difference between href=# and href=javascript:void(0). Friends who need it can refer to it

#"contains a location information

The default anchor point is #top is the top of the webpage

And javascript:void(0) only represents a dead link

This is why sometimes the page is very long and the browsing link is obviously #but it jumps to the top of the page

This is not the case with javascript:void(0)

So it is best to use void(0)

or

# when calling the script ##

Wait

Several ways to open a new window link

1.window.open('url')

2.Use a custom function

  <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=""
Copy after login

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

If it is a #, there will be a jump To reach the top situation, several solutions from personal collection:


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>(好像在FF中不能显示)
Copy after login

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

Use JavaScript with caution:void(0)

void is an operator in JavaScript, which specifies that an expression should be calculated but does not return a value.

void operator. The usage format is as follows:

1. javascript:void (expression)
2. javascript:void expression
Copy after login

expression is a JavaScript standard expression to be evaluated. The parentheses outside the expression are optional, but it is a good habit to write them using the void operation. specifies a hyperlink. The expression will be evaluated but nothing will be loaded into the current document. The code below creates a hyperlink and nothing will happen when the user clicks the link. void(0 ) is calculated as 0, but has no effect on JavaScript


<a href="javascript:void(0)">单击此处什么也不会发生</a>
Copy after login

That is to say, if you want to perform some processing without refreshing the page as a whole, you can use void(0), but When you need to refresh the page, you need to be careful.

In fact, we can use . The sentence will perform a submit operation. So under what circumstances is void(0) used more often? Without refresh, of course it is Ajax. If you look at the Ajax web page, you will generally see a lot of void(0), :), so when using void(0) ), it’s best to think about whether this page needs to be refreshed as a whole.

The above is the detailed content of Analysis of the difference between a tag href=# and href=javascript:void(0) in html. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template