Javascript: void (0) Probe _ javascript skills

Source: Internet
Author: User
If you want to do nothing after clicking a link, or respond to the click and complete other tasks, it is generally better to set the property href & quot; # & quot, the following describes several common solutions for you. If you want to create a link and click it on the webpage, you can also set the href = "#" attribute to respond to the click and complete other tasks. However, when the page has a scroll bar, the page will return to the top of the page after clicking it, poor user experience.

There are currently several solutions:
1) do not do anything after clicking the link

The Code is as follows:


Test
Test
Test // use 2 to 4 #. For details, refer to "####" and "# all ".


2) click the link to respond to user-defined click events

The Code is as follows:


Test
All problems are solved, including browser incompatibility. // or you can directly use href = ""
Test


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

The Code is as follows:


Click Me!


Void is a javascript operator that only executes expressions but does not return values,
The format of the void operator is as follows:

The 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 it does not load any content in the current document. void (0) is calculated as 0, but it has no effect on JavaScript. That is to say, the effect is the same.
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.

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.