Javascript:void (0)

Source: Internet
Author: User

Javascript:void (0)

In JavaScript, Void is an operator that specifies that an expression is evaluated but does not return a value.

The void operator uses the following format:

1. javascript:void (expression)

2. Javascript:void expression

Expression is a standard of Javascript to evaluate. Parentheses on the outside of the expression are optional, but writing is a good habit.

You use the void operator to specify a hyperlink. The expression is evaluated but does not load any content at 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 on the link, the page rolls up to the top of the page, # The default anchor point is #TOP (the actual test finds that the scrollbar rolls to the top) and the above four methods simply indicate that a dead link does not jump or return to the top.

Example-why does Location.href not jump automatically?

<a href= "javascript:void (0)" onclick= "Delete (' 123 ')" > Delete </a>

function Delete (ID) {

if (Confirm ("Do you want to delete [why Location.href does not jump automatically?") ]? ")) {

Location.href= "/delete.jsp?id=" + ID;

}

}

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

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

<a href= "Javascript:delete (' 123 ')" > Delete </a>function Delete (id) {

if (Confirm ("Do you want to delete [why Location.href does not jump automatically?") ]? ")) {

Location.href= "/delete.jsp?id=" + ID;

}

}

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

Because void is an operator, an expression is evaluated, but no value is returned, and of course it does not change anything on the current page and will not jump normally.

Description

The void operator evaluates 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 the result.

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

Javascript:void (0)

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.