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 ;"
We often use click events in tag.