The Wayback Machine - https://web.archive.org/web/20241126020727/https://www.geeksforgeeks.org/html-dom-location-href-property/
Open In App

HTML DOM Location href Property

Last Updated : 03 Aug, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 the href property.
location.href
  • It is used to set the href property.
location.href = URL

Example 1: Below programs illustrate the Location href Property in HTML

html




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM Location href Property</title>
    <style>
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
   
    <h2>DOM Location href Property</h2>
   
    <p>
        For returning the entire URL current page,
        double click the "Return URL" button:
    </p>
    <button ondblclick="myhref()">
      Return URL
      </button>
   
    <p id="href"></p>
   
    <script>
        function myhref() {
            let h = location.href;
            document.getElementById("href").innerHTML = h;
        }
    </script>
</body>
 
</html>


Output:

Example 2: Set the href value to point to another URL of the website. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM Location href Property</title>
    <style>
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
  <h1>GeeksforGeeks</h1>
     
  <h2>DOM Location href Property</h2>
     
  <p>
        For redirecting to GeeksforGeeks homepage,
        double click the "Redirect" button:
     
  </p>
     
  <button ondblclick="myhref()">
      Redirect
      </button>
     
  <script>
        function myhref() {
            location.href =
                "https://www.geeksforgeeks.org";
        }
    </script>
</body>
 
</html>


Output:

Supported Browsers: The browser supported by Location href Property are listed below:

  • Google Chrome 1
  • Edge 12
  • Firefox 1
  • Opera 12.1
  • Safari 1


Previous Article
Next Article

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
How to set location and location.href using JavaScript ?
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
1 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' <!DOCTYPE html> <html lang="en"> <head> <title>Example 1:</title> </head
2 min read
HTML | DOM Base href Property
The DOM Base href Property in HTML DOM is used to set or return the value of the href attribute of a <base> element. The href attribute is used to specify the base URL for all the relative URL in the webpage. Syntax: It returns the href property. baseObject.href It is used to set the href property. baseObject.href = URL Property Values: It co
2 min read
HTML | DOM Link href Property
The HTML | DOM Link href Property is used to set or return the URL of a Linked Document. Syntax: It return the href property.linkObject.hrefIt is used to Set the href property.linkObject.href = URL Property Values: It contains the value i.e URL which specify the URL of the link. absolute URL: It points to another website.relative URL: It points to
2 min read
HTML DOM Area href Property
The Area href Property in HTML DOM is used to set or return the value of the href attribute. It is used to specify the URL of the area. For example: https://manaschhabra:manaschhabra499@www.geeksforgeeks.org/ (manaschhabra is the username and manaschhabra499 is the password). Syntax: It returns the href property. areaObject.hrefIt is used to set th
2 min read
HTML DOM Anchor href Property
The Anchor href Property in HTML DOM is used to set or return the value of href attribute of a link. The href attribute is used to specify the link's destination. Syntax: It returns the href property. anchorObject.hrefIt is used to set the href property. anchorObject.href = URL Property Values: It contains the value i.e. URL which specify the URL o
2 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
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
HTML DOM Location hostname Property
The Location hostname property in HTML is used to return the hostname of the current URL. The Location hostname property returns a string that contains the domain name, or the IP address of a URL. Syntax: It returns the hostname property. location.hostname It is used to set the hostname property. location.hostname = hostname The below program illus
1 min read
HTML DOM Location host Property
The Location Host property in HTML is used to sets or return the hostname and port of a URL. The Location Hash property doesn't return the port number if it is not specified in the URL. Syntax: It returns the host property. location.hostIt is used to set the host property. location.host = hostname:portProperty Value: hostname:port: It contains a st
1 min read
HTML DOM Location hash Property
The Location Hash property in HTML is used to return the anchor part of a URL. It can also be used to set the anchor part of the URL. It returns the string which represents the anchor part of a URL including the hash '#' sign. Syntax: It returns the hash property.location.hashIt is used to set the hash property. location.hash = anchorname Property
1 min read
HTML DOM Location pathname Property
The Location pathname property in HTML is used to set or return the pathname of a URL. The Location pathname property returns a string that represents the pathname of the URL. Syntax: It returns the pathname property.location.pathnameIt is used to set the pathname property.location.pathname = pathExample: Below program illustrates the Location path
1 min read
HTML DOM Location protocol Property
The Location protocol property in HTML is used to return the protocol or set the protocol of the current URL. It returns a string that contains the protocol of the current URL, including the colon (:). Syntax: It returns the protocol property. location.protocolIt is used to set the protocol property. location.protocol = protocolProperty Value: This
1 min read
HTML DOM Location port Property
The HTML DOM Location Port property returns or sets the port number of the current URL. Syntax: Get the port property:Set the port property: Property Value: A String, to be assigned as a port Return Value: A String represents the port number of a URL. Example: In this example, we will use the HTML DOM Location Port. C/C++ Code <!DOCTYPE html>
1 min read
HTML | DOM Location origin Property
The DOM Location origin Property in HTML is used to return the protocol, hostname and port number of a URL. This is a read-only property. Syntax: location.origin Return Value: This method returns a String value representing the protocol, the domain name (or IP address) and port number. The URL's having the 'file://' protocol may return different va
1 min read
HTML | DOM Location Search Property
The Location Search property in HTML DOM is used to set or return the query part of URL including question mark. The query part is the part of URL after the question mark. Syntax: It returns the location search property.location.searchIt is used to set the location search property.location.search = searchString Property Value: searchString which is
1 min read
HTML | DOM KeyboardEvent location Property
The KeyboardEvent location property is used for returning a number that indicates the location of a key on the keyboard or device. The KeyboardEvent location property can be used on the onkeydown and onkeyup events but not on onkeypress. The number returned by KeyboardEvent location property is represented by 4 constants: ConstantLocationDescriptio
2 min read
HTML DOM document location Property
The Document location property returns a Location of the document, that contains information about the URL of the document. This is a read-only property. Syntax: loc = document.location; Return Value: This property returns the string URL location of the document. Example: In this example, we will learn how to get the Document Location URL using thi
1 min read
HTML DOM Window.location Property
HTML DOM Window.location property returns a Location object that contains information about the current location of the document. Window.location is read-only property. Syntax: var loc = window.location; Return Value: An object containing information about the location of the document. Example: This example shows how to get the location of the docu
1 min read
SVG AElement.href Property
The SVG AElement.href property returns an SVGAnimatedLength object corresponding to the attribute of the given A element Syntax: AElement.href Return value: This property returns SVGAnimatedLength object that can be used to get the href of the AElement. Example 1: <!DOCTYPE html> <html> <body> <svg viewBox="0 0 100 100
1 min read
HTML | <link> href Attribute
The HTML <link> href Attribute is used to specify the URL of the linked document. It mostly contains the URL of the stylesheet file. Syntax: <link href="URL"> Attribute Values: It contains the value i.e URL which specifies the URL of the linked document. Possible values are: absolute URL: It points to another website.Relative URL: iT po
1 min read
HTML | <area> href Attribute
The <area> href attribute is used to specify the URL of the target page. When the href attribute is not present in the <area> an element that it will not be a hyperlink. Syntax: <area href="URL"> Attribute Value: absolute URL: It points to another website.relative URL: It points to a file within a website.anchor URL: It points to
1 min read
How href attribute is different from src attribute in HTML ?
In HTML5, the href and src attributes play distinct roles in defining paths or URLs, each associated with specific HTML elements. The href attribute, commonly found in an anchor (<a>) elements, points to the destination of hyperlinks, facilitating navigation. The src attribute, used with elements like <img> and <script>, specifies
1 min read
HTML <base> href Attribute
HTML <base> href Attribute specifies the base URL for all relative URLs in a document, serving as the reference point for resolving relative links and resources. Syntax:  <base href="URL">Attribute Values: It contains the value i.e URL which specifies the absolute URL that defines the base URL of the page. for eg."www.geeksforgeeks.org"
1 min read
HTML <a> href Attribute
The HTML <a> href attribute is used to specify the URL of the page that the link points to. When the href attribute is not present in the <a> element, it will not function as a hyperlink. This attribute is essential for creating links to any address and is used in conjunction with the <a> tag. Syntax<a href="URL"> Link text
1 min read
HTML href Attribute
HTML href Attribute: It is used to specify the URL of the document. It contains the full address of the page that the user requests. It is used to link or connect one document to another document. Supported tags:HTML <a> href AttributeHTML <base> href AttributeHTML <area> href AttributeHTML <link> href AttributeHTML <a
3 min read
HTML | DOM Location replace() Method
The DOM Location replace() Method in HTML is used to replace the current document with the specified one. This method is different from the assign() method as this removes the current document from the document history, therefore it is not possible to go back to the previous document using the 'back' button. Syntax: location.replace( newUrl ); Para
1 min read
HTML DOM Location assign( ) Method
The Location assign() method is used for loading a new document. It can also be used for loading a new document but the difference between these two methods is that the replace() method removes the URL of the current document from the document history and therefore it is not possible to navigate back to the original document using the "back" button
1 min read
HTML DOM Location Object
The Location Object in HTML DOM is used to provide information about the current URL of a webpage. This object is a sub part of a window object. As it get the information by using window.location property. The location Object is used to retrieve the information regarding the hash value , host _name , port number and name of the protocol and so on.
3 min read
Article Tags :
three90RightbarBannerImg