Javascript:void (0) What is the meaning and href= #与href =javascriptvoid (0) The difference _javascript skills

Source: Internet
Author: User

In JavaScript, Void is an operator that specifies that you want to evaluate an expression but not return a value.

void operator usage format is as follows:

1. javascript:void (expression)

2. Javascript:void expression

Expression is an expression of the Javascript standard to compute. The parentheses on the outer side of the expression are optional, but it is a good habit to write.

You specify the hyperlink using the void operator. The expression is evaluated but no content is loaded at the current document.

Example-click Hyperlink do 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>

When you click on the link, the page rolls up to the top of the page, # The default anchor is #TOP (the actual test finds that the scroll bar rolls to the top) and the above four methods only indicate that a dead link does not jump or return to the top.

Example-why location.href does not automatically jump?

<a href= "javascript:void (0)" onclick= "Delete (' 123 ')" > Delete </a>
function Delete (id) {
 if (confirm) (" Are you sure you want to delete [why Location.href does not automatically jump? ]? ")) {
  location.href="/delete.jsp?id= "+ ID;
 }
}

The above code no matter how to check there is no problem, and location.href= "/delete.jsp?id=" + ID; in other places, why is this code OK?

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

<a href= "Javascript:delete (' 123 ')" > Delete </a>function Delete (id) {
 if confirm ("You are sure you want to delete [ Why does location.href not automatically jump? ]? ")) {
  location.href="/delete.jsp?id= "+ ID;
 }
}

We found that the page immediately jump, can normally delete the corresponding data. Why?

Because void is an operator, it evaluates an expression but does not return a value, and certainly does not change anything on the current page and does not jump normally.

Description

The void operator is evaluated over 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 this result.

Link (href) direct use of javascript:void (0) in IE may cause some problems, such as: causing GIF animation to stop playing, so the safest way is to use "#". To prevent clicking on the link to jump to the top of the page, the OnClick event return false.

The difference of ps:href= #与href =javascriptvoid (0)

# "contains a location information
The default anchor point is #top, which is the top of the page.
and javascript:void (0) only represents a dead link
This is why sometimes the page is very long to browse the link obviously is the # but beat to the top of the page
and javascript:void (0)
It's not so, so it's better to invoke a script with void (0)
or <input onclick> <div onclick>, etc.

Several ways to open a new window link

1.window.open (' url ')

2. Use 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> 

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

If it is a #, there will be jumps to the top of the situation, the personal collection of several solutions:

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> (as if not shown in FF)

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

The above content is small make up to everybody introduced javascript:void (0) is what meaning and href= #与href =javascriptvoid (0) difference, hope to be helpful to everybody.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.