
- 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
JavaScript - Redirect a URL
To redirect a URL using JavaScript, the code is as follows −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } </style> </head> <body> <h1>JavaScript Redirect a URL</h1> <div class="sample"></div> <button class="Btn">CLICK HERE</button> <h3> Click on the above button to redirect the url to www.google.com </h3> <script> document.querySelector('.Btn').addEventListener('click',()=>{ window.location.href = 'https://www.google.com'; }) </script> </body> </html>
Output
On clicking the ‘CLICK HERE’ button −
- Related Articles
- How to use window.location to redirect to a different URL with JavaScript?
- How to automatically redirect a Web Page to another URL?
- How to use window.location to redirect to a different URL?
- How to redirect URL to the different website after few seconds?
- How to redirect to another webpage using JavaScript?
- How to redirect to another webpage with JavaScript?
- How to redirect if JavaScript is not enabled in a browser?
- How to use JavaScript to redirect a webpage after 5 seconds?
- How do I redirect my web page with JavaScript?
- How to use JavaScript to redirect an HTML page?
- How to decode a URL using JavaScript function?
- How to encode a URL using JavaScript function?
- How to create a URL object using JavaScript?
- Writing a custom URL shortener function in JavaScript
- Converting images to a Base64 data URL using Javascript

Advertisements