JavaScirpt Tutorial Index

JavaScript Tutorial Javascript Example Javascript comment Javascript Variable Javascript Data Types Javascript Operators Javascript If Else Javascript Switch Statement Javascript loop JavaScript Function Javascript Object JavaScript Arrays Javascript String Javascript Date Javascript Math Javascript Number Javascript Dialog Box Javascript Window Object Javascript Document Object Javascript Event Javascript Cookies Javascript getElementByID Javascript Forms Validation Javascript Email Validation Javascript Password Validation Javascript Re-Password Validation Javascript Page Redirect Javascript Print How to Print a Page Using JavaScript Textbox events in JavaScript How to find the largest number contained in a JavaScript Array?

Misc

JavaScript P5.js JavaScript Minify JavaScript redirect URL with parameters Javascript Redirect Button JavaScript Ternary Operator JavaScript radio button checked value JavaScript moment date difference Javascript input maxlength JavaScript focus on input Javascript Find Object In Array JavaScript dropdown onchange JavaScript Console log Multiple variables JavaScript Time Picker Demo JavaScript Image resize before upload Functional Programming in JavaScript JavaScript SetTimeout() JavaScript SetInterval() Callback and Callback Hell in JavaScript Array to String in JavaScript Synchronous and Asynchronous in JavaScript Compare two Arrays in JavaScript Encapsulation in JavaScript File Type Validation while Uploading Using JavaScript or jquery Convert ASCII Code to Character in JavaScript Count Character in string in JavaScript Get First Element of Array in JavaScript How to convert array to set in JavaScript How to get current date and time in JavaScript How to Remove Special Characters from a String in JavaScript Number Validation in JavaScript Remove Substring from String in JavaScript

Interview Questions

JavaScript Interview Questions JavaScript Beautifier Practice Javascript Online Object in JavaScript JavaScript Count HTML Interpreter Getters and Setters in JavaScript Throw New Error in JavaScript XOR in JavaScript Callbacks and Promises in JavaScript Atob in JavaScript Binary in JavaScript Palindrome Number in JavaScript How to Get First Character Of A String In JavaScript How to Get Image Data in JavaScript How to get URL in JavaScript JavaScript GroupBy Methods difference-between-var-let-and-const-keyword-in-java-script JavaScript Beautifier Iterating over Objects in Javascript Find the XOR of two numbers without using the XOR operator

Javascript Page Redirect

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