A few ways to call JS in a label

Source: Internet
Author: User
We often have click events in the A tab:
1. A href= "Javascript:js_method ();"

This is a common approach on our platform, but this approach can easily go wrong when passing this parameter, and javascript: The protocol as the HREF attribute of a will not only lead to unnecessary triggering of window.onbeforeunload events, but will also stop the animated GIF pictures in IE. The standard of the consortium does not recommend JavaScript statements to be executed in href

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

This method is a lot of web site most commonly used method, is also the most comprehensive method, the OnClick method is responsible for the implementation of JS function, and void is an operator, void (0) return undefined, address does not occur jump. Moreover, this method does not expose the JS method directly to the status bar of the browser as in the first method.

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

This method is similar to 2 kinds, the difference is only the execution of an empty JS code.

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

This method is also a very common online code, #是标签内置的一个方法, representing the role of top. So in this way, click on the Back page and return to the top of the page.

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

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

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

To synthesize the above, the most appropriate method of calling 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;"

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.