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/>