×

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 clicked a URL to reach a page X but internally it directed us to another page Y. It happens due to Page redirection.

How Page Redirect Works.

The implementation of Page redirect are as follows:

Example 1

When we use JavaScript at client side it is quite simple to use of Page Redirect. Need to add a line in head section to redirect the site visitors to a new page.
<!DOCTYPE html>  
<html>  
<head>  
<script type="text/javascript">  
function Redirect() {  
window.location="http://www.tutorialandexample.com";  
}  
</script>  
</head>  
<body>  
<p>Click the following button, you will be redirected to home page.</p>  
<form>  
<input type="button" value="Redirect" onclick="Redirect();" />  
</form>  
</body>  
</html>
Try Now

Example 2

To show a convenient message to visitors before redirecting them to new page. We use setTimeout() is built-in JavaScript function which is used to execute function after a given interval of time.
<!DOCTYPE html>  
<html>  
<head>  
<script type="text/javascript">  
function Redirect() {  
window.location="http://www.tutorialsandexample.com";  
}  
document.write("You will be redirected to main page in 10 sec.");  
setTimeout('Redirect()', 10000);  
</script>  
</head>  
</html>
Try Now

Example 3

In this example shows how to redirect the visitors onto a different page based on their browsers.
<!DOCTYPE html>  
<html>  
<head>  
<script type="text/javascript">  
varbrowsername=navigator.appName;  
if(browsername == "Netscape" )  
{  
window.location="http://www.location.com/ns.htm";  
}  
else if ( browsername =="Microsoft Internet Explorer")  
{  
window.location="http://www.location.com/ie.htm";  
}  
else  
{  
window.location="http://www.location.com/other.htm";  
}  
</script>  
</head>  
</html>
Try Now

Related Topics

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.

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.

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 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 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 Forms Validation

Form validation works at server, after client had entered all the necessary details and then pressed submit button. JavaScript provides the facility to validate the form on the client side...

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

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

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

Minify JavaScript

What do you mean by the term ‘Minification’? The technique of minification reduces the amount of code and markup in your websites and scripting files. It's one of the most used...

4 minutes 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 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 dropdown onchange

What is onchange event? It is an event in JavaScript used to make the web pages dynamic. The onchange event gets triggered whenever the value of an event changes. It usually...

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