Several Methods for calling Js in tag

Source: Internet
Author: User

We often use click events in tag:
1. a href = "javascript: js_method ();"

This is a common method on our platform, but this method is prone to problems when passing this parameter, and javascript: when the protocol is used as the href attribute of a, not only does it cause unnecessary window triggering. the onbeforeunload event stops playing GIF animated images in IE. The W3C standard does not recommend executing JavaScript statements in href.

2. a href = "javascript: void (0);" onclick = "js_method ()"

This method is the most common and comprehensive method for many websites. The onclick method is used to execute JS functions, and void is an operator. Void (0) returns undefined, the address does not jump. In addition, this method does not directly expose the js method to the browser's status bar like the first method.

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

This method is similar to the two methods. The difference is that an empty js code is executed.

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

This method is also a common code on the Internet. # is a built-in label method, which represents the role of top. Therefore, after clicking this method, the page is returned to the top of the page.

5. a href = "#" onclick = "js_method (); Return false ;"

In this method, return false after the JS function is executed. The page does not jump and is still in the current position after execution.

I read the home page of Taobao. They use 2nd methods, while Alibaba uses 1st methods, the difference is that the Javascript methods in each href are surrounded by try and catch.

Based on the above, the most appropriate method to call JS functions 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 ;"

In JS methods, the string type must be enclosed by quotation marks.

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.