×

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

Cookie is a piece of data which is sent from a website and stored locally by the user's browser. Cookie is needed because HTTP is stateless protocol. Today most of...

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 Variable

Variables are used to store values (name="Ram") or expressions (Sum=x+y). Before using of variable first we need to declare it. We use keyword var to declare a variable like this: var name; There are two types of variables: Local Variable ...

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

The concept of operators is the basis of nearly all programming languages used in today's computing systems. Operators facilitate the execution of certain functions. For instance, the (+) symbol is...

8 minutes read.

Functional Programming in JavaScript

Most developers work with object-oriented programming while developing their applications, but sometimes they need to change their approach to solve some specific tasks. Although mostly we tend to go for...

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

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