Use of javascript: void (0)

Source: Internet
Author: User

When you do a page, if you want to do nothing after clicking a link, or respond to the click to complete other tasks, you can set its property href = "#", however, in this case, when a page has a scroll bar, clicking it will return to the top of the page, and the user experience is poor.

There are currently several solutions:
1) do not do anything after clicking the link
Copy codeThe Code is as follows:
<A href = "javascript: void (0);"> test </a>
<A href = "javascript:;"> test </a>
<A href = "####"> test </a> // use 2 to 4 #. For details, see "####", you can also use "# all" and other

2) click the link to respond to user-defined click events
Copy codeThe Code is as follows:
<A href = "javascript: void (0)" onclick = "doSomething ()"> test </a>
<A href = "#" onclick = "doSomething (); return false;"> all problems have been solved, including browser incompatibility issues </a> // or use href = ""
<A href = "#" onclick = "alert (); event. returnValue = false;"> test </a>

Note:
1. javascript: the pseudo-Protocol void (0) is rarely written. If you have read some standard web books, you will know why. (Do not understand, the original words are extracted, and the record is made for now)
2. 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.
3. You can use
Copy codeThe Code is as follows:
<Span style = "cursor: pointer" onclick = "foo ()"> Click Me! </Span>

Void is a javascript operator that only executes expressions but does not return values,
The format of the void operator is as follows:
Copy codeThe Code is as follows:
Javascript: void (expression)
Javascript: void expression

We recommend that you use the second type with parentheses for a good program style.
We can use the void operator to specify the hyperlink, such as javascript: void (document. form. submit ()). The expression is calculated, but no content is loaded in the current document. void (0) is calculated as 0, but it has no effect on JavaScript, that is, <a href = "javascript: the effect of void (0) "> is the same as that of <a href =" javascript: void (1) ">.
The key is as long as you know that void is the operator of javascipt itself, it indicates that only expressions are executed, but no return value is returned!

In addition, the page is automatically adjusted back to the top because "#" is the top position by default, so this situation occurs.

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.