×

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

Array to String in JavaScript

As a web developer, you often have to deal with multiple data types in a single application. How to convert an array to a string in JavaScript is essential for...

19 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 Number

The Number object is a wrapper object which allows you to work with numerical values. The number object is created using the Number() constructor.It may be integer or floating-point. Syntax var n=new Number(value); Examples <!DOCTYPE html>   <html>   <body>   <script>   var x=100;//integer value   var y=100.7;//floating point value   var z=12e5;//exponent value, output: 1200000   var n=new Number(20);//integer value by number object     document.write(x+" "+y+" "+z+" "+n);   </script>   </body>   </html> Try Now Output 100 100.7 1200000 20 Description The primary...

1 minute 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 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.

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.

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 redirect URL with parameters

Data can be often passed between web pages as information in URL parameters or query strings. A URL (Uniform Resource Locator) can have a single parameter or multiple parameters.Parameters can...

4 minutes read.

JavaScript Arrays

JavaScript Arrays: JavaScript Arrays are a type of variables that allows us to store the individual or the group of values under a single variable. What is an Array in JavaScript? The array...

6 minutes read.

Javascript comment

Introduction In programming languages, comments are a method to include meaningful annotations within the code that the compiler or interpreter will ignore at run time. That is to say, though comments...

7 minutes read.

Javascript Tutorial

JavaScript is a light-weight, dynamic scripting language that is mainly employed to create interactive front-end web applications. It was originally developed in 1995 by Brendan Eich at Netscape Communications Corporation...

5 minutes read.

Javascript Document Object

Document object represents the whole HTML documents. When HTML document is loaded in browser it becomes the document object. It is the root elements that represent the HTML documents. With the help of...

2 minutes read.

Javascript Date

In JavaScript Date Object is data type build into the JavaScript language. Data object are created with the new Date(). JavaScript Date instance that represents a single moment in time. JavaScript...

2 minutes read.

Javascript Window Object

The window object represents a window in browser. An object of window is created automatically by the browser. Window is the object of browser, it is not the object of JavaScript. The...

3 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 Print

JavaScript help's the functionality using the print function of window object. In JavaScript print function window.print() print the current web page when it executed. We can call function directly using onclick...

1 minute read.

JavaScript Image resize before upload

Image resizing can be a quite tedious task, so it is usually done on the server-side. The resized image file is then delivered to the client side. However, sometimes the...

3 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 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 Function

An important part of JavaScript is the ability to create new functions within <script>.....</script> tag. To declare a function in JavaScript using function keyword. We call JavaScript function multiple times to reuse the...

3 minutes read.