A comparison between the href attribute and onclick event of the a tag

A comparison between the href attribute and onclick event of the a tag

First of all, let's talk about the execution order of the href attribute and the onclick event. When the mouse clicks the a tag, the onclick event will be executed first, and then the action under the href attribute (page jump, or javascript pseudo link). If you don't want to execute the action under the href attribute, onclick needs to return false, which is usually written like onclick="xxx();return false.

How to execute javascript code expressions and standard writing in href attributes

"In JavaScript, void is an operator that specifies that an expression is to be evaluated but no value is to be returned.

The void operator usage format is as follows:

1. javascript: void (expression)
2. javascript:void expression

expression is a JavaScript-standard expression to be evaluated. The parentheses around the expression are optional, but it is good practice to write them. (Implemented in Navigator 3.0)

You can specify hyperlinks using the void operator. The expression will be evaluated but nothing will be loaded into the current document. ”

href=# contains a location information. The default anchor is #top, which is the top of the web page, while javascript:void(0) only indicates a dead link without any information. So it is best to use void(0) when calling the script

href usually points to a URL address, and can also call javascript, such as href="javascript:xxx();". The document recommends writing it like this: <a href="javascript:void(0)" onclick="xxx();">xx</a>, but this method sometimes causes strange problems in complex environments. Try not to use javascript: protocol as A's href attribute, which will not only lead to unnecessary triggering of window.onbeforeunload event, but also stop playing gif animation pictures in IE.

<<:  Some conclusions on the design of portal website focus pictures

>>:  Detailed process of deploying Docker to WSL2 in IDEA

Recommend

Specific usage of CSS compound selectors

Intersection Selector The intersection selector i...

Complete steps of centos cloning linux virtual machine sharing

Preface When a Linux is fully set up, you can use...

A brief discussion on MySQL event planning tasks

1. Check whether event is enabled show variables ...

How does the MySQL database implement the XA specification?

MySQL consistency log What happens to uncommitted...

js implements a simple calculator

Use native js to implement a simple calculator (w...

Detailed tutorial on installing Nginx 1.16.0 under Linux

Because I have been tinkering with Linux recently...

MySQL data operation-use of DML statements

illustrate DML (Data Manipulation Language) refer...

Complete steps for mounting a new data disk in CentOS7

Preface I just bought a new VPS. The data disk of...

Sample code for deploying ELK using Docker-compose

environment Host IP 192.168.0.9 Docker version 19...

The pitfalls and solutions caused by the default value of sql_mode in MySQL 5.7

During normal project development, if the MySQL v...

Node.js sends emails based on STMP protocol and EWS protocol

Table of contents 1 Node.js method of sending ema...

About Vue's 4 auxiliary functions of Vuex

Table of contents 1. Auxiliary functions 2. Examp...