The difference between the "turn" onclick event and the href= ' javascript:function () '

Source: Internet
Author: User

Href= ' javascript:function () ' and onclick can have the same effect, in general, if you want to invoke the script or write code in the onclick event, but not recommended in href= ' javascript:function () ' This way, because the href attribute is set in the JS code, in some browsers may cause other unnecessary events. Cause unintended effects.

And the onclick event will be executed first than the HREF attribute, so it will trigger the onclick and then trigger the href, so if you do not want the page to jump, you can set the onclick inside the JS code execution to return a false, so that the contents of the href will not be executed.

In an AJAX application, write the following to indicate that the link does not jump and executes a JS script.
<a href= "javascript:void (0)," onclick= "function ()" ></a>
or <a href= "javascript:;" onclick= "function ()" ></a>

void (0) is only used to calculate a null value, but in fact it is nothing to do, and the semicolon ";" is an empty JS statement, so that no other jumps occur, and the standard is not recommended to execute JavaScript inside the href, so it is best to use the OnClick event trigger

"Go" onclick event differs from href= ' javascript:function () '

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.