×

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

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 setTimeout()

The setTimeout() method creates a timer that, when it expires, runs a function or provides a piece of code. setTimeout() is an asynchronous function, which means that it will not interrupt...

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

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 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 Math

Math is build-in object that has properties and methods for mathematical constants and functions. It allows you to perform mathematical tasks on numbers. Syntax varpi_val = Math.PI; varsin_val = Math.sin(30); Examples Math.pow() Math.pow(x,y) returns the value of x to the power...

2 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.

Javascript Password Validation

Here we see that how to validate any password field. Like password field can be blank and length of password is minimum 8 characters. Example <!DOCTYPE html>   <html>   <head>   <script>   functionpass_validation()   {     var password=document.myform.password.value;     if (password==null || password=="")   {     alert("password can't be blank");     return false;     }   else if(password.length<8)   {     alert("Password must be at least 8 characters long.");     return false;       }     }     </script>   </head>   <body>   <form name="myform" method="post" action="register.php" onsubmit="return pass_validation()" >   Password: <input type="password" name="password">   <input type="submit" value="submit">   </form>   </body>   </html> Try Now ← Prev Next → ...

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

String object works with series of characters. It is used to store and manipulate text. There are two ways to create string in JavaScript: String literal. Using new keyword. String literal By using double quotes...

4 minutes read.

JavaScript moment date difference

Often date differences are required on the web platforms for various reasons. They can be: To find the duration, of course, someone is pursuingTo find the age of a user from...

7 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 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 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 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 radio button checked value

What are radio buttons? A radio button is defined as an icon that is used to take input from the user. The user can choose only one option(value) from the group...

5 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 Switch Statement

Introduction Conditional statements are simple building blocks in programming languages and are utilized fairly frequently. This portion of Developing Conditional Statements in JavaScript talks about how to utilize the if, else,...

5 minutes read.

Javascript Event

Events are things that happen, usually user action, that are associated with an object. All object have properties and methods. Some objects also have events. The event handler is a command that is...

1 minute read.

Javascript Object

An object is an entity having state and behavior. JavaScript is an object oriented scripting language. JavaScript is template based not class based but we can create object directly. Syntax to...

2 minutes read.