Several methods of calling js in a are sorted out and recommended for use

Several methods of calling js in a are sorted out and recommended for use
We often use click events in the a tag:

1. a href="javascript:js_method();"

This is a commonly used method on our platform, but this method is prone to problems when passing parameters such as this. In addition, when the javascript: protocol is used as the href attribute of a, it will not only cause unnecessary triggering of the window.onbeforeunload event, but will also stop the gif animation from playing in IE. W3C standards do not recommend executing JavaScript statements in href

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

This method is the most commonly used method for many websites and is also the most comprehensive method. The onclick method is responsible for executing the js function, and void is an operator. void(0) returns undefined and the address does not jump. And this method will not directly expose the js method in the browser status bar like the first method.

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

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

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

This method is also a very common code on the Internet. # is a method built into the tag, representing the function of top. So using this method, after clicking on the web page, you will return to the top of the page.

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

This method returns false after clicking and executing the js function, the page does not jump, and the page remains at the current position after execution.

I looked at Taobao's homepage. They used the second method, while Alibaba's homepage used the first method. The difference between them is that each javascript method in the href is surrounded by try and catch.

To sum up, the most appropriate method to call js function in a is recommended:

Copy code
The code is as follows:

a href="javascript:void(0);" onclick="js_method()"
a href="javascript:;" onclick="js_method()"
a href="#" onclick="js_method();return false;"

<<:  A brief discussion on Flink's fault-tolerant mechanism: job execution and daemon

>>:  Detailed explanation of JavaScript's garbage collection mechanism

Recommend

MySQL DeadLock troubleshooting full process record

【author】 Liu Bo: Senior Database Manager at Ctrip...

Vue implements drag and drop or click to upload pictures

This article shares the specific code of Vue to a...

JavaScript to implement slider verification code

This article shares the specific code of JavaScri...

How to support full Unicode in MySQL/MariaDB

Table of contents Introduction to utf8mb4 UTF8 by...

MySQL 5.7.18 installation tutorial under Windows

This article explains how to install MySQL from a...

MySQL Community Server 5.7.19 Installation Guide (Detailed)

MySQL official website zip file download link htt...

Pure CSS and Flutter realize breathing light effect respectively (example code)

Last time, a very studious fan asked if it was po...

How to shrink the log file in MYSQL SERVER

The transaction log records the operations on the...

Solution for applying CSS3 transforms to background images

CSS transformations, while cool, have not yet bee...

7 native JS error types you should know

Table of contents Overview 1. RangeError 2. Refer...

Graphic tutorial for installing MySQL 5.6.35 on Windows 10 64-bit

1. Download MySQL Community Server 5.6.35 Downloa...

How to install and modify the initial password of mysql5.7.18 under Centos7.3

This article shares with you the installation of ...

Summary of problems encountered when installing docker on win10 home version

Docker download address: http://get.daocloud.io/#...

JavaScript implementation of verification code case

This article shares the specific code for JavaScr...