Open In App

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. 

Below are the predefined properties and methods that are given below.



Properties:  

Methods: 



Note : All major browsers supports all properties and methods of location object. 

Below example illustrates how to access the properties and method of location objects. 

Example 1:  Below code returns the protocol, port number and the host name of the URL.




<!DOCTYPE html>
<html lang="en">
<body>
    <h1 style="color:green">GeeksforGeeks</h1>
    <b>DOM Location origin property</b>
  
    <button onclick="getOrigin();">
        Get Location Origin
    </button>
  
    <p>The location origin of this page is:</p>
  
    <div class="location"></div>
  
    <script>
        function getOrigin() {
            let loc = location.origin;
            document.querySelector(
                '.location').innerHTML = loc;
        }
    </script>
</body>
</html>

Output:

Example 2: Below code illustrates the use of assign() method in location object.  




<!DOCTYPE html>
<html>
  
<head>
    <title>Location assign() Method in HTML</title>
  
    <style>
        h1 {
            color:green;
        }
          
        h2 {
            font-family:Impact;
        }
          
        body {
            text-align:center;
        }
    </style>
</head>
  
<body>
  
    <h1>GeeksforGeeks</h1>
    <h2>Location assign() Method</h2>
  
    <p>
        For loading a new document, double 
        click the "Load Document" button:
    </p>
  
    <button ondblclick="load()">
        Load Document
    </button>
  
    <script>
        function load() {
            location.assign("https://www.geeksforgeeks.org");
        }
    </script>
</body>
</html>
                     

Output:

Supported Web Browsers:


Article Tags :