The Wayback Machine - https://web.archive.org/web/20240718130039/https://www.geeksforgeeks.org/how-to-set-location-and-location-href-using-javascript/
Open In App

How to set location and location.href using JavaScript ?

Last Updated : 13 Jan, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
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";

or

location.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:

 



Similar Reads

Difference between window.location.href, window.location.replace and window.location.assign in JavaScript
Window.location is a property that returns a Location object with information about the document's current location. This Location object represents the location (URL) of the object it is linked to i.e. holds all the information about the current document location (host, href, port, protocol, etc.) All three commands are used to redirect the page t
4 min read
HTML DOM Location href Property
The Location href property in HTML is used to set or return the complete URL of the current page. The Location href property can also be used to set the href value point to another website or point to an email address. The Location href property returns a string that contains the entire URL of the page, including the protocol. Syntax: It returns th
2 min read
How to make workaround for window.location.href?
Given a URL, the task is to use the current address of the page and to perform an operation using this address. Example 1: This example points to the same URL and only redirects to the same address.URL = 'https://gitstr.herokuapp.com' &lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt; &lt;title&gt;Example 1:&lt;/title&gt; &lt;/head
2 min read
Which href value should we use for JavaScript links, "#" or "javascript:void(0)" ?
In this article, we will know which "href" should be used for JavaScript when we can use "#" or "javascript:void(0)". Let us understand it. Using the '#' value: The # is a fragment identifier used to refer to a specific section of the same document. When a user clicks a link with a # href, the browser will scroll to that section of the page specifi
3 min read
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. The document.location is read-only in Internet Expl
2 min read
How to hide span element if anchor href attribute is empty using JavaScript ?
The task is to hide the &lt;span&gt; tag when href attribute of &lt;a&gt; tag is not present or invalid. Let us consider we have &lt;a&gt; tag inside the &lt;span&gt; tag. It will look similar to this: &lt;span&gt;&lt;a href="www.google.com"&gt;&lt;/a&gt;&lt;/span&gt; Now we add the id to the elements to identify them. &lt;span id="outer"&gt;&lt;a
2 min read
How to change the href value of a tag after click on button using JavaScript ?
JavaScript is a high-level, interpreted, dynamically typed, and client-side scripting language. HTML is used to create static web pages. JavaScript enables interactive web pages when used along with HTML and CSS. Document Object Manipulation (DOM) is a programming interface for HTML and XML documents. DOM acts as an interface between JavaScript and
4 min read
JavaScript location.host vs location.hostname
In this article, we will learn about how to get information related to the webpage like hostname, port number, etc. We will also see the difference between the location.hostname and location.host. location object is used to get information about the current web page. host and hostname are properties of location objects. location.hostname: This prop
2 min read
How to insert a JavaScript variable inside href attribute?
To insert a JavaScript variable inside the href attribute of an HTML element, &lt;a &gt; ... &lt;/a&gt; tags are used. One of the attributes of 'a' tag is 'href'. href: Specifies the URL of the page the link goes to. Below are the Methods to use Variables inside this 'href' attribute: Table of Content Approach 1: Using onclick propertyApproach 2: U
3 min read
How to set cursor style to pointer for links without href ?
Method 1: Using a CSS class for the links A CSS class is created that changes the cursor property. The cursor property is used to specify the mouse cursor to be displayed when the mouse is pointed over an element. Using the 'pointer' value in this property will change the cursor to a 'pointer' indicating a link. This class can then be used on any l
2 min read
three90RightbarBannerImg