Javascript: What does void (0) mean and the difference between href = # And href = javascriptvoid (0,

Source: Internet
Author: User

Javascript: What does void (0) mean and the difference between href = # And href = javascriptvoid (0,

In Javascript, void is an operator that specifies to calculate an expression but does not return a value.

The format of the void operator is as follows:

1. javascript: void (expression)

2. javascript: void expression

Expression is a standard Javascript expression to be calculated. Parentheses outside the expression are optional, but it is a good habit to write them.

You use the void operator to specify the hyperlink. The expression is calculated, but no content is loaded in the current document.

Example-click a hyperlink and 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>

After clicking the link, the page will be rolled up to the TOP, # The default anchor is # TOP (the scroll bar will be rolled to the TOP in the actual test) the above four methods only indicate that a dead Link means that a dead link will not jump or return to the top.

Example-why is location. href not automatically redirected?

<A href = "javascript: void (0)" onclick = "delete ('000000')"> delete </a> function delete (id) {if (confirm ("Do you want to delete [Why location. does href fail to jump automatically?] ? ") {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; it can be used in other places. Why can this code be used?

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

<A href = "javascript: delete ('20140901')"> delete </a> function delete (id) {if (confirm ("Do you want to delete [Why location. does href fail to jump automatically?] ? ") {Location. href ="/delete. jsp? Id = "+ id ;}}

We found that the page jumps immediately and the corresponding data can be deleted normally. Why?

Void is an operator that calculates an expression but does not return a value. Of course, it does not change any content on the current page, so it does not jump normally.

Description

The void operator evaluates the expression and returns undefined. This operator is most useful when you want to evaluate the expression but do not want the rest of the script to see this result.

The direct use of javascript: void (0) by Link (href) in IE may cause some problems, such as: causes the GIF animation to stop playing. Therefore, the safest way is to use "#". To prevent the link from jump to the top, The onclick event return false.

PS: href = # differences from href = javascriptvoid (0)

# "Contains a location information
The default anchor is # top, that is, the top of the web page.
Javascript: void (0) only indicates a dead link.
This is why sometimes a long page browsing link is clearly # But the page jumps to the top
Javascript: void (0)
This is not the case. Therefore, it is best to use void (0) when calling the script)
Or <input onclick> <div onclick>

How to open a new window Link

1. window. open ('url ')

2. Use udfs

<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 is a #, it will jump to the top of the situation, personal collections 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> (it seems that it cannot be displayed in FF)

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

The above content is the javascript: void (0) and the difference between href = # And href = javascriptvoid (0). I hope it will help you.

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.