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

Example code for drawing double arrows in CSS common styles

1. Multiple calls to single arrow Once a single a...

A brief discussion on the application of Html web page table structured markup

Before talking about the structural markup of web...

How to make your own native JavaScript router

Table of contents Preface Introduction JavaScript...

Comprehensive explanation of CocosCreator hot update

Table of contents Preface What is Hot Change Coco...

An example of how to implement an adaptive square using CSS

The traditional method is to write a square in a ...

Complete steps of centos cloning linux virtual machine sharing

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

Implementing Markdown rendering in Vue single-page application

When rendering Markdown before, I used the previe...

Detailed summary of web form submission methods

Let's first look at several ways to submit a ...

How to view and close background running programs in Linux

1. Run the .sh file You can run it directly using...

Realize super cool water light effect based on canvas

This article example shares with you the specific...

Example of how to create a local user in mysql and grant database permissions

Preface When you install MySQL, you usually creat...

A brief analysis of the use of the HTML webpack plugin

Using the html-webpack-plugin plug-in to start th...

14 techniques for high-performance websites

Original : http://developer.yahoo.com/performance...

Detailed explanation of the adaptive adaptation problem of Vue mobile terminal

1. Create a project with vue ui 2. Select basic c...

Data URI and MHTML complete solution for all browsers

Data URI Data URI is a scheme defined by RFC 2397...