How the href in the tag calls the JS code __javascript

Source: Internet
Author: User

The href attribute of the,<a> tag in HTML is used to specify the URL of the hyperlink's destination. In all browsers, the default appearance of the link is:

Links that are not accessed are underlined and blue the links that have been visited with underlined and purple active links are underlined and red (note: Active links are "links that are being opened")

Examples are shown below:

<p>
<a href= "/index.html" > This text </a> is a link to a page in this website. </p>

<p><a href= "http://www.microsoft.com/" > This text </a> is a link to a page on the World Wide Web. </p>

When the <a> tag calls the code in JS, there are several ways to do this:

<a href = "Javascript:js_method ()" > Text </a>
This is a commonly used method on the platform, but this method can easily be problematic 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, in IE will make the GIF animation stop playing. The standard of the consortium does not recommend executing JavaScript statements within HREF.
<a href = "javascript:void (0);" onclick = "Js_method ()" > Text </a>
This is a lot of web site most commonly used method, but also the most comprehensive method, the onclick is responsible for the implementation of JS function, and void is an operator, void (0) return undefined, address does not occur jump, and this method will not be like the first method of the JS method directly exposed to the browser status bar
<a href = "javascript:;" onclick = "Js_merthod ()" > Text </a>
This approach is similar to the 2nd, the difference is only the execution of an empty JS code.
<a href = "#" onclick = "Js_method ()" >
This method is also a very common online code, #是标签内置的一个方法, representing the role of top. So this method clicks on the back page and returns to the top of the page.
<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.

Taobao's homepage is a 2nd approach, and Alibaba uses the 1th method, and our difference is that each of the JavaScript methods in the href is surrounded by try,catch.

Synthesis of the above,<a> tags to call the JS function appropriate method recommended to use:

<a href = "javascript:void (0);" onclick = "Js_method ()" >
<a href = "javascript:;" onclick = "Js_method ()" >
<a href = "#" onclick = "js_method (); return false" >
For example, use the Method 1:

<! DOCTYPE html>



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.