How to redirect to a relative URL in JavaScript? Last Updated : 30 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report To redirect to a relative URL in JavaScript, you could use window.location.href. It is a property in JavaScript that represents the complete URL of the current page. It can be used to get the current URL or to navigate to a new URL by assigning a new URL to it. Approach HTML Structure:The HTML file includes a simple button with the text "Redirect." When the button is clicked, it triggers the redirectToRelativeURL function.JavaScript Function (redirectToRelativeURL):The redirectToRelativeURL function is defined in the <script> tag.It begins by specifying the relative URL ('geeksforgeeks.org') to which the page should redirect.Construct Absolute URL:The function uses the URL object to construct an absolute URL based on the current location (window.location.href) and the specified relative URL.The URL object takes care of handling relative paths and ensures the resulting URL is valid.Redirect:Finally, the function sets window.location.href to the constructed absolute URL, causing the browser to navigate to the specified location.Example: Below is the implementation. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content= "width=device-width, initial-scale=1.0"> <title>Redirect Example</title> </head> <body> <h2>Redirect Example</h2> <button onclick="redirectToRelativeURL()">Redirect</button> <script> // Function to redirect to a relative URL function redirectToRelativeURL() { // Relative URL to redirect to const relativeURL = 'https://www.geeksforgeeks.org/'; // Construct absolute URL based // on the current location const absoluteURL = new URL(relativeURL, window.location.href); // Log the absolute URL (optional) console.log('Redirecting to:', absoluteURL.href); // Redirect to the absolute URL window.location.href = absoluteURL.href; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article JavaScript Redirect a URL D divyanksingh200 Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2019 JavaScript-Questions +1 More Similar Reads How to Get Browser to Navigate URL in JavaScript? As a web developer, you may need to navigate to a specific URL from your JavaScript code. This can be done by accessing the browser's window object and using one of the available methods for changing the current URL.In JavaScript, there are several approaches for navigating to a URL. The most common 4 min read JavaScript Redirect a URL Redirecting a URL in JavaScript is nothing but sending the user from a URL to another URL. In Javascript, window.location function is used to redirect to a URL. Example 1:HTML<!DOCTYPE html> <html> <head> <title>Redirect url in Javascript</title> </head> <body 1 min read How to redirect back to original URL in Node.js ? Node.js with the help of Express, supports web page routing. This means that when the client makes different requests, the application is routed to different web pages depending upon the request made and the routing methods defined. To learn more about Node.js routing and its implementation, refer t 3 min read How to Redirect to Another Webpage using JavaScript? JavaScript can redirects the users from current page to another webpage (different URL) with and without requiring any manual action (like clicking a link). To redirect to another weboage in JavaScript we need to manipulate the window.location object and it will allow us to navigate programmatically 2 min read How To Get URL And URL Parts In JavaScript? In web development, working with URLs is a common task. Whether we need to extract parts of a URL or manipulate the URL for navigation, JavaScript provides multiple approaches to access and modify URL parts. we will explore different approaches to retrieve the full URL and its various components.The 3 min read How to Open URL in New Tab using JavaScript? To open a URL in a new tab using JavaScript, we can use the window.open() method. The window.open() method is used to open a new browser window or tab. It can also be used to load a specific URL in a new tab. Syntaxwindow.open(URL, '_blank');window.open(): Opens a new tab or window.First Parameter: 3 min read Like