
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to redirect to another webpage using JavaScript?
The window.location object contains the current location or URL information. We can redirect the users to another webpage using the properties of this object. window.location can be written without the prefix window.
We use the following properties of the window.location object to redirect the users to another webpage −
window.location.href- it returns the URL (href) of the current page.
window.location.replace()- it replaces the current document with new document.
window.location.assign() loads a new document.
The syntaxes below are used to redirect to another web page. We omit the window prefix and use only location in all the program examples. You can try running the programs using full name i.e. window.location.
Syntaxes
location.href = "url"; location.replace("url"); location.assign("url");
Parameters
url- it’s the URL of the new web page.
Redirect using location.href
The window.location.href property can be used to redirect to another page. We can use the following syntaxes to redirect -
Window.location.href = "url"; Location.href = "url"; location = "url";
The above-mentioned syntaxes are equivalent to each other you can use any of the syntaxes for your purpose of redirecting the users to another web page.
Let’s understand with the help of a complete example −
Example
In the example below, we redirect the users to another webpage (www.tutorix.com) using location.href property.
<html> <head> <script type="text/javascript"> function Redirect() { location.href="https://www.tutorix.com"; } </script> </head> <body> <p>Click the following button to redirect to tutorix.com.</p> <form> <input type="button" value="Redirect Me" onclick="Redirect();" /> </form> </body> </html>
Output
When you run the above program, it will produce the following result -
When you click on the “Redirect Me” button, it will redirect to the new web page.
Redirect using location.replace() method
The location.replace(url) method can be used to replace the current webpage with another webpage loaded from the url. We can use the following syntaxes to redirect −
Window.location.replace("url"); Location.replace("url")
Both the syntaxes mentioned above are equivalent to each other we can use any of the syntaxes for our purpose of redirecting the users to another web page.
Let’s understand with the help of a complete example-
Example
In the example below, we redirect the users to another webpage (www.tutorix.com) using location.replace(url) method.
<html> <head> <script type="text/javascript"> function Redirect() { location.replace("https://www.tutorix.com"); } </script> </head> <body> <p>Click the following button to redirect to tutorix.com.</p> <form> <input type="button" value="Redirect Me" onclick="Redirect();" /> </form> </body> </html>
Output
When you run the above program, it will produce the following result −
When you click on the “Redirect Me” button, it will redirect to the new web page.
Redirect using location.assign()
The location.assign(url) method loads the resource provided in the url. It can be used to redirect to another webpage. We can use the following syntaxes to redirect −
Window.location.assign("url"); Location.assign("url")
Both the syntaxes mentioned above are the same, we can use any of the syntaxes for our purpose of redirecting to another web page.
Example
In the example below, we redirect the users to another webpage (https://www.totorix.com) using location.assign(url) method.
<html> <head> <script type="text/javascript"> function Redirect() { location.assign("https://www.tutorix.com"); } </script> </head> <body> <p>Click the following button to redirect to tutorix.com.</p> <form> <input type="button" value="Redirect Me" onclick="Redirect();" /> </form> </body> </html>
Output
When you run the above program, it will produce the following result −
When you click on the “Redirect Me” button, it will redirect to the new webpage.
- Related Articles
- How to redirect to another webpage with JavaScript?
- How to redirect to another webpage using jQuery?
- How to use JavaScript to redirect a webpage after 5 seconds?
- How to link a submit button to another webpage using HTML?
- How to create Pay Roll Management Webpage using JavaScript?
- Bash Terminal Redirect to another Terminal
- How to automatically redirect a Web Page to another URL?
- How to find Elements in a Webpage using JavaScript in Selenium?
- How to use JavaScript to redirect an HTML page?
- How to make a page redirect using jQuery?
- How to use window.location to redirect to a different URL with JavaScript?
- How to add Summernote Editor to the webpage in Javascript?
- How to use JavaScript to load a webpage after 5 seconds?
- How to redirect if JavaScript is not enabled in a browser?
- How to redirect website after certain amount of time without JavaScript?
