×

Javascript Redirect Button

What is a redirect button?

When a button acts as a hyperlink, it is known as a redirect button. On clicking the button, it transfers the user to another page.

How to make a redirect button in JavaScript?

There are various ways to create a redirect link in JavaScript, the most common ways are using the following attributes:

  • replace() attribute

It stimulates an HTTP Direct.

Syntax:

window.location.replace("url of the website/document");

Example:

<!DOCTYPE html>
<html>
<head>
    <title>
         Javascript redirect buttons
    </title>
</head>


<body>


<h2>Javascript Redirect Button using replace() </h2>
<p>The following example demonstrates how the replace() method is used to replace the current document with a new one.</p>


<button onclick="myFunctiondemo()">Redirect Button</button>


<script>
function myFunctiondemo() {
  location.replace("https://www.javatpoint.com/")
}
</script>


</body>
</html>

The above code can also be written in a slightly different way:

<!DOCTYPE html>
<html>
<head>
    <title>
         Javascript redirect buttons
    </title>
</head>


<body>
<h2>Javascript Redirect Button using replace() </h2>
<p>The following example demonstrates how the replace() method is used to redirect the current web page to a new one.</p>


<button id="redirect">Redirect Button</button>


<script type="text/javascript">




 document.getElementById("redirect").onclick = function () {
    location.replace("https://www.javatpoint.com/")
};
</script>


</body>
</html>

Output:

Javascript Redirect Button

After clicking the button, the web page is now redirected to the Javatpoint website,

Javascript Redirect Button
  • href attribute:

It stimulates on a mouse click.

Syntax:

window.location.href  = "url of the website/document";

Example:

<!DOCTYPE html>
<html>
<head>
    <title>
         Javascript redirect buttons
    </title>
</head>


<body>
<h2>Javascript Redirect Button using href attribute</h2>
<p>The following example demonstrates how the href() attribute is used to redirect the current web page to a new one.</p>


<button onclick="myFunctiondemo()">Redirect Button</button>


<script>
function myFunctiondemo() {
  location.href = ("https://www.javatpoint.com/";
}
</script>


</body>
</html>

The above code can also be written in a slightly different way:

<!DOCTYPE html>
<html>
<head>
    <title>
         Javascript redirect buttons
    </title>
</head>


<body>
<h2>Javascript Redirect Button using href attribute</h2>
<p>The following example demonstrates how the href() attribute is used to redirect the current web page to a new one.</p>


<button id="redirect">Redirect Button</button>


<script type="text/javascript">




 document.getElementById("redirect").onclick = function () {
  location.href = ("https://www.javatpoint.com/");
};
</script>


</body>
</html>

Output:

Initially, the screen looks like this:

Javascript Redirect Button

After clicking the button, the web page is now redirected to the Javatpoint website,

Javascript Redirect Button

What is the difference between replace() and href?

The major difference between href and replace, is that replace() can remove the URL of the current document or the current website from the history. It means that it is impossible navigate back to the original document, using the "back" button.

NOTE: Tags like the anchor tag <a> can also be used to create redirect buttons. However, no JavaScript is required in it.


Related Topics

What is Ternary Operator in JavaScript?

In some cases, a ternary operator can be used instead of an if-else expression. A ternary operator examines a condition and then runs a block of code in response to...

4 minutes read.

Javascript Email Validation

We can validate the email with the help of JavaScript. Here we check the condition related to any email id, like email it must have "@" and "." sign and...

2 minutes read.

Javascript loops

Loops in JavaScript: Loops are used to execute a specific piece or block of code repeatedly until the given condition is satisfied. What is a Loop? Almost all loops contain a certain...

7 minutes read.

Design a BMI calculator using JavaScript

BMI calculator BMI stands for Body Mass Indicator. It is a numeric value which is calculated based on the height and weight of a person. It represents the fatness of the...

4 minutes read.

Javascript If Else

Introduction The if-else statement is an important control structure for decision-making in code based on some conditions. It gives programmers the ability to add decision-making functionality into their programs. Using if-else,...

6 minutes read.

Javascript Dialog Box

A JavaScript dialog box is predefined function which is used to perform different task. Some functions are used in JavaScript Dialog box. FunctionDescriptionalert()To give alert message to userprompt()To input value from...

2 minutes read.

Callback and Callback Hell in JavaScript

The widely used client-side programming language JavaScript is simple, light, and interpretive. JavaScript is used by the majority of client-side web applications. It is also possible to use JavaScript on...

3 minutes read.

D3.js Tutorial

Introduction of D3.js D3.js is referred to as a JavaScript library that is used to manipulate the documents according to the data. The intensity of D3 is based on web standards...

5 minutes read.

Javascript Example

What is JavaScript? JavaScript is a highly versatile programming language. It provides the capability to add interactivity to static web pages, which are designed with HTML and CSS. It also provides...

5 minutes read.

Javascript Redirect Button

What is a redirect button? When a button acts as a hyperlink, it is known as a redirect button. On clicking the button, it transfers the user to another page. How to...

3 minutes read.

Javascript Data Types

An Overview of JavaScript Data Types In computing, there are a number of data types, including numbers, strings, and booleans, to name a few. The role of these data types in...

5 minutes read.

JavaScript focus on input

In JavaScript, the elements can be focused using either focus() method or onfocus() event. What is focus() method? When we want to focus on an element (if it can be focused) or...

4 minutes read.

JavaScript setInterval()

On the Window and Worker interfaces, the setInterval() method calls a function or executes a snippet with a specified time delay between each call. This function provides an interval ID that...

4 minutes read.

Javascript Find Object In Array

What is find() method? It returns the value of the first element in the given array that fulfils the given testing function. However, if no values from the array are able...

3 minutes read.

Javascript input maxlength

Various attributes often accompany the input tag in HTML to change the properties of the input field. Maxlength() is one such attribute. The maxLength attribute defines the maximum number of characters...

2 minutes read.

Javascript getElementByID

The document.getElementById() method returns the element of specified id. In below example we receive input field by using document.getElementById() method, here document.getElementById() receive input value on the basis of input field...

1 minute read.

JavaScript p5.js

Introduction to p5.js P5.js is a JavaScript library for creative programming. It is built on Processing, a coding environment for creativity. Processing's major goal is to make it as simple as...

4 minutes read.

Javascript Page Redirect

The window.location object can be used to get the current page address (URL) and to redirect browser to new page. In some websites when we visit, we face a situation where we...

3 minutes read.

JavaScript Console log Multiple variables

What does console.log() do in JavaScript? The console.log() is a function in JavaScript language. It is used to: Print any message in the console which is visible to the userPrint the values...

3 minutes read.

JavaScript Time Picker Demo

Time Pickers in JavaScript are lightweight and mobile-friendly controls that let the users enter or select date and time values. These values can be from a pop-up calendar or a...

5 minutes read.