How to set location and location.href using JavaScript ? Last Updated : 24 Jul, 2025 Comments Improve Suggest changes 2 Likes 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: Create Quiz Comment S shivam70 Follow 2 Improve S shivam70 Follow 2 Improve Article Tags : Technical Scripter JavaScript Web Technologies Technical Scripter 2020 JavaScript-Questions +1 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like