How to prevent hyperlink redirection using JavaScript (multiple ways of writing)

How to prevent hyperlink redirection using JavaScript (multiple ways of writing)

Through JavaScript, we can prevent hyperlinks from jumping.

Here’s how:

(1) Manipulating the href attribute of a hyperlink

Writing method 1:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <a href="javascript:void(0);" rel="external nofollow" >Hyperlink</a>
</body>
</html>

Writing method 2:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <a href="javascript:;" rel="external nofollow" >Hyperlink</a>
</body>
</html> 

Clicking the link does not redirect.

(2) Default behavior of blocking links

Writing method 1:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<a href="https://www.baidu.com" rel="external nofollow" rel="external nofollow" >Baidu</a>
	<script>
		var link = document.querySelector("a");
		link.addEventListener('click',function(e){
			e.preventDefault();
		})
	</script>
</body>
</html>

Writing method 2:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<a href="https://www.baidu.com" rel="external nofollow" rel="external nofollow" >Baidu</a>
	<script>
		var link = document.querySelector("a");
		link.onclick = function (e) {
			return false;
		}
	</script>
</body>
</html> 

At this time, clicking the hyperlink will not jump.

The above is the details of how to use JavaScript to prevent hyperlink jumps (various writing methods). For more information about js hyperlink jumps, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Solution to the problem of Chinese garbled characters in hyperlink jumps between pages in JS
  • How to disable hyperlink redirect in javascript
  • How to get the absolute URL address of a hyperlink using JavaScript
  • How to correctly call a javascript function with a hyperlink
  • Four forms of parameter passing - URL, hyperlink, js, form
  • How to handle the default event of a tag hyperlink in javascript
  • js implements the method of submitting form with a tag hyperlink

<<:  Tutorial on installing Android Studio on Ubuntu 19 and below

>>:  Summary of tips for setting the maximum number of connections in MySQL

Recommend

Docker's four network types principle examples

Four network types: None: Do not configure any ne...

jQuery implements a simple comment area

This article shares the specific code of jQuery t...

Implementing simple chat room dialogue based on websocket

This article shares the specific code for impleme...

How to upload and download files between Linux server and Windows system

Background: Linux server file upload and download...

Teach you how to make cool barcode effects

statement : This article teaches you how to imple...

Problems and solutions for installing Docker on Alibaba Cloud

question When installing Docker using Alibaba Clo...

JavaScript to make the picture move with the mouse

This article shares the specific code of JavaScri...

Problems encountered in the execution order of AND and OR in SQL statements

question I encountered a problem when writing dat...

How to avoid data loop conflicts when MySQL is configured with dual masters

I wonder if you have ever thought about this ques...

Analysis of the difference between emits and attrs in Vue3

Table of contents in conclusion Practice Analysis...

Complete steps to configure IP address in Ubuntu 18.04 LTS

Preface The method of configuring IP addresses in...

Detailed explanation of Linux tee command usage

The tee command is mainly used to output to stand...

What to do if you forget your password in MySQL 5.7.17

1. Add skip-grant-tables to the my.ini file and r...