Invoking JavaScript code in HTML tags <a/>

Source: Internet
Author: User

The "href" attribute of the <a/> tag can be a valid URL that represents the destination of the jump, and in addition, the href can be a piece of JavaScript code. When setting JavaScript code for "href", the format is as follows: <a href= "javascript: ...;"/>. When you click on this form of hyperlink, the browser executes the JavaScript code set in "href". Note that if the return value after execution of the code is a valid value (other than "undefined"), the contents of the current page will be replaced by the return value!

It is not recommended to use this method to invoke JavaScript, which can lead to problems such as triggering unnecessary "onbeforeunload" events, and so on. We can use some of the methods recommended below to make it more safe to call JavaScript code.

Mode one: <a href= "javascript:void (0);" onclick= "..."/>. Use the void operator to avoid returning a valid value, and the void operator returns "undefined" for any value.

Way two: <a href= "javascript:;" onclick= "..."/>. With an empty JavaScript statement, no valid values are returned.

Way three: <a href= "#" onclick= "...; return false "/>. Cancels the default behavior of the element that triggered the event by returning "false" in the event handler.

Appendix:<a/> the effect of assigning different values to the label "href" attribute.

Href Effect
A valid URL Jumps to the resource that the URL points to.
"#" Jumps to the top of the current page.
"#XXX" "XXX" is the Name property value of the other <a/> tag on this page, or the value of the ID of the other element. Jumps to where the corresponding element in the page is located.
"" A blank string, which causes the current page to reload when clicked.

Invoking JavaScript code in HTML tags <a/>

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.