javascript:void(0) meaning and usage examples

javascript:void(0) meaning and usage examples

Introduction to void keyword

First of all, the void keyword is a very important keyword in JavaScript. This operator specifies that an expression is to be calculated or run, but no value is returned.

Syntax format:

  • void func()
  • void(func())

Example 1

When clicking the hyperlink, nothing happens

<!-- 1. When the user links, void(0) is calculated as 0, and the user's click will not have any effect-->
    <a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" >Click here and nothing will happen</a><br>

Example 2

Execute the code in the void operator

<!-- 2. Execute the code in the void() operator -->
    <a href="javascript:void(alert('You're really obedient!!! Hahaha!!!'))" rel="external nofollow" >Click me to pop up a warning box!!!</a><br>
    <a href="javascript:void(console.log('You're really obedient!!! Hahaha!!!'))" rel="external nofollow" >Click me to output to the console!!!</a><br>

Example 3

Computing Arithmetic with Void Operators

html:

<button type="button">Click me</button>

js:

<script type="text/javascript">
        document.querySelector('button').addEventListener('click', function () {
            var a, b, c, d;
            a = void(b = 2, c = 3);
            console.log('a => ' + a);
            console.log('b => ' + b);
            console.log('c => ' + c);
            console.log('d => ' + d);
        });
    </script>

Console output:

Obviously, 2 is assigned to a, 3 is assigned to b, and a is just defined like d, so it is undefined.

# Difference from javascript:void(0):

#You can jump to the destination with the set id

javascript:void(0) stays where it is, motionless, we call it a "dead link"

As in the following example:

<a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" >You can click, I won’t move anyway, hehehe! ! ! </a><br>
    <a href="#destination" rel="external nofollow" >Click me to jump</a>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <h2 id="destination">Destination</h2>
  • When I click the first link, nothing happens.
  • When you click the second link, you will be redirected to the location of the specified ID (the bottom of the page)

This concludes this article about the meaning and usage examples of javascript:void(0). I hope it will be helpful for everyone’s study, and I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Explanation of the meaning of javascript void (0) and the difference between it and "#"
  • Detailed explanation of the usage of javascript:void(0) in js
  • javascript:void(0) function example
  • A brief discussion on the difference between href=# and href=javascript:void(0)

<<:  Sharing of research experience on using characters instead of rounded corners and sharp corners

>>:  Detailed explanation of MYSQL stored procedure comments

Recommend

Docker View JVM Memory Usage

1. Enter the host machine of the docker container...

jquery+springboot realizes file upload function

This article example shares the specific code of ...

Use of nginx custom variables and built-in predefined variables

Overview Nginx can use variables to simplify conf...

How to set mysql5.7 encoding set to utf8mb4

I recently encountered a problem. The emoticons o...

CentOS8 installation tutorial of jdk8 / java8 (recommended)

Preface At first, I wanted to use wget to downloa...

How to deeply understand React's ref attribute

Table of contents Overview 1. Creation of Refs ob...

js+css to realize three-level navigation menu

This article example shares the specific code of ...

Detailed explanation of the relationship between Linux and GNU systems

Table of contents What is the Linux system that w...

MySQL 5.6 installation steps with pictures and text

MySQL is an open source small relational database...

How to reference external CSS files and iconfont in WeChat applet wxss

cause The way to import external files into a min...

Detailed explanation of MySQL date addition and subtraction functions

1. addtime() Add the specified number of seconds ...

You Probably Don’t Need to Use Switch Statements in JavaScript

Table of contents No switch, no complex code bloc...

Determine the direction of mouse entry based on CSS

In a front-end technology group before, a group m...