How to set location and location.href using JavaScript ? Last Updated : 30 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will set the location and location.href using Javascript.Both location and location.href are used to set or return the complete URL of your current page. They return a string that contains the entire URL with the protocol.Syntax:location = "https://www.geeksforgeeks.org";orlocation.href = "https://www.geeksforgeeks.org";Both are used to set the URL. Both are described as running JavaScript 1.0 in the backend in Netscape 2.0 and have been running in all browsers ever since. However, you have the liberty to prefer any one of the two according to your convenience but it is preferred to use location.href because the location might not support older versions of Internet Explorer. Commands like location.split("#); cannot be used as the location is an object but location.href can be used as it is a string. Example: The following code demonstrates the DOM Location href property. HTML <style> h1 { color: green; } h2 { font-family: Impact; } body { text-align: center; } </style> <body> <h1>GeeksforGeeks</h1> <h2>Setting location and location.href</h2> <p> Click on the button to go to designated URL </p> <button ondblclick="myhref()"> Destination URL </button> <p id="href"></p> <script> function myhref() { location.href = "https://www.geeksforgeeks.org"; } </script> </body> Output: Comment More infoAdvertise with us Next Article How to Open URL in New Tab using JavaScript? S shivam70 Follow Improve Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2020 JavaScript-Questions +1 More Similar Reads JavaScript | window.location and document.location Objects window.location and document.location: These objects are used for getting the URL (the current or present page address) and avert browser to a new page or window. The main difference between both is their compatibility with the browsers. The window.location is read/write on all compliant browsers. T 2 min read How to replace plain URL with link using JavaScript ? Given a plane URL, the task is to replace the plain URLs with the links. This problem can be solved with the help of Regular Expressions. Approach:Â Using RegExp and replace() MethodRegExp - This looks for the URL in the provided text.This RegExp parses the URL and puts the address to the $1 variable 2 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 How to redirect to a relative URL in JavaScript? 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 2 min read How to navigate URL in an iframe with JavaScript ? To navigate the URL in an iframe with JavaScript, we have to set the src attribute or return the value of the src attribute in an iframe element. The src attribute defines the URL of the document that can be shown in an iframe.Syntax:document.getElementById("selector").src = "URL";URL values: Absolu 1 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 Like