Ways to implement various click (onclick) events in a tag

Source: Internet
Author: User

We used to have click events in a tag:
1. A href= "Javascript:js_method ();"

This is a common method on our platform, but this method is prone to problems when passing parameters such as this, and JavaScript: The protocol as the HREF attribute of a will not only cause unnecessary triggering of the Window.onbeforeunload event, but will also stop the animated GIF image from playing in IE. The standard is not recommended to execute JavaScript statements in href

2. A href= "javascript:void (0);" onclick= "Js_method ()"

This method is the most common method for many websites , but also the most comprehensive method, the OnClick method is responsible for executing the JS function, and void is an operator, void (0) returns undefined, the address does not occur jump. And this method does not directly expose the JS method to the browser's status bar, as in the first method.

3.a href= "javascript:;" onclick= "Js_method ()"

This method is similar to that of 2, except that it executes an empty JS code.

4.a href= "#" onclick= "Js_method ()"

This method is also very common online code, #是标签内置的一个方法, representing the role of top. So use this method to return to the top of the page after clicking on the page.

5.a href= "#" onclick= "Js_method (); return false;"

This method clicks Execute JS function and return false, the page does not jump, after execution or in the current position of the page.

I looked at Taobao's homepage, they adopted the 2nd method, and Alibaba's homepage is the 1th method, and the difference is that each href JavaScript method is surrounded by a try, catch.

Synthesizing the above, the most appropriate method of invoking the JS function in a is recommended:
a href= "javascript:void (0);" onclick= "Js_method ()"
A href= "javascript:;" Onclick= "Js_method ()"
A href= "#" onclick= "Js_method (); return false;"

How to implement various click (onclick) events in the A label

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.