a href onclick usage

Source: Internet
Author: User
 

The difference between using JavaScript in the href and onclick of a label


The ⒈ link onclick event is executed first, followed by the action under the HREF attribute (page jump, or JavaScript pseudo link);

⒉ assume that there is both href and onclick in the link, and if you want the action under the HREF attribute to not execute, the onclick must get a false return value;

⒊ If the page has a scroll bar too long and you want to perform the action through the linked onclick event. Its href attribute should be set to Javascript:void (0) instead of #, which prevents unnecessary page runout;

⒋ If you call a function with a return value in the href attribute of the link, the contents of the current page will be replaced by the return value of the function;

⒌ is different when you hold down the SHIFT key.

⒍ IE6.0 in the form of an href to parentnode. Try not to use JavaScript: protocol as the HREF attribute of a, this will not only cause unnecessary trigger window.onbeforeunload event, in IE will make GIF animated pictures stop playing. It took a lot of time on this.

(Under IE6)

<a href= "javascript:void (0);" onclick= "Javascript:modifypassword ();" ></a>

This kind of writing IE6 the following URL although requested backstage, but no movement at the front desk,

The correct wording should be

<a href= "javascript:void (0);" onclick= "Javascript:modifypassword (); return false;" ></a>

A few examples of personal collections for you to learn:
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)---------------------------------------------------- ---------------------------

Careful with javascript:void (0) today when the test, obviously the program has been executed, and the final result is correct, but the page is not refreshed.

In FireFox2.0 and IE7 under the test, the result is normal, IE6 but not refreshing. After careful investigation, found that the page link is <a href= "javascript:void (0)" onclick= "Xxx_func ();" ....> test </a> The problem is on this void (0)!

Let's take a look at the meaning of Void (0) in javascript: JavaScript void is an operator that specifies that you want to evaluate an expression but not return a value. The
void operator usage format is as follows:
1 javascript:void (e-xpression)
2. Javascript:void e-xpression
E-xpression is a calculated A JavaScript-standard expression. The parentheses outside the expression are optional, but writing is a good habit. We can use the void operator to specify a hyperlink. The expression is evaluated but does not load any content in the current document. Surface code creates a hyperlink that will not happen when the user clicks on it. When the user clicks on the link, void (0) evaluates to 0, but there is no effect on JavaScript. <a href= "javascript:void (0)" > Click here Nothing happens </a> that is, you can use void (0) to perform some processing, but not to refresh the page as a whole. But if you need to refresh the page, then be careful. In fact, we can use <a href= "javascript:void (Document.form.submit)", this sentence will be a submit operation. What is the case with void (0) More? No refresh, of course, Ajax, look at the AJAX Web page, you will generally see a lot of void (0),: So in the use of void (0) before, it is best to think about whether the page needs to be overall refreshed.

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.