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

Disabled values ​​that cannot be entered cannot be passed to the action layer

If I want to make the form non-input-capable, I se...

Summary of some situations when Docker container disk is full

Preface This article describes two situations I h...

Implementation example of Nginx+Tomcat load balancing cluster

Table of contents introduction 1. Case Overview 2...

Beginners learn some HTML tags (3)

Beginners who are exposed to HTML learn some HTML...

Detailed explanation of PHP+nginx service 500 502 error troubleshooting ideas

Overview When a 500 or 502 error occurs during ac...

Install nvidia graphics driver under Ubuntu (simple installation method)

Install the nvidia graphics card driver under Ubu...

How to set the page you are viewing to not allow Baidu to save its snapshot

Today, when I searched for a page on Baidu, becaus...

Detailed instructions for installing SuPHP on CentOS 7.2

By default, PHP on CentOS 7 runs as apache or nob...

Implementation of HTML sliding floating ball menu effect

CSS Styles html,body{ width: 100%; height: 100%; ...

VUE implements timeline playback component

This article example shares the specific code of ...

Linux common text processing commands and vim text editor

Today, let's introduce several common text pr...

Detailed installation tutorial of Mysql5.7.19 under Centos7

1. Download Download mysql-5.7.19-linux-glibc2.12...

JavaScript Timer Details

Table of contents 1. Brief Introduction 2. setInt...

The forgotten button tag

Note: This article has been translated by someone ...

Three methods to modify the hostname of Centos7

Method 1: hostnamectl modification Step 1 Check t...