Thursday, September 4, 2025
HomeLanguagesJavascriptHow to get the current URL using JavaScript ?

How to get the current URL using JavaScript ?

In this article, we will know about getting the Website URL for the current webpage or website. The current URL can be obtained by using the ‘URL’ property of the Document object which contains information about the current URL. The ‘URL’ property returns a string with the full location of the current page, along with containing the string having the HTTP protocol such as ( http://).

We can get the current URL using Javascript in two ways:

Document.URL: The DOM URL property in HTML is used to return a string that contains the complete URL of the current document. The string also includes the HTTP protocol such as ( http://).

Syntax:

document.URL

Return Value: It returns a string value that represents the full URL of the document. 

Example 1: This example illustrates to get the current URL of the webpage.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to get the current URL using JavaScript ?
    </title>
</head>
  
<body>
    <h1 style="color:green">neveropen</h1>
    <h2>
        JavaScript: Program to
        get the website URL ?
    </h2>
    <p>
        <b>
            Click on Below Button
            To Get Current URL
        </b>
    </p>
  
    <button onclick="GetURL()">
         Get URL 
    </button>
    <p id="url"></p>
  
  
    <script>
        function GetURL() {
            var gfg = document.URL;
            document.getElementById("url").innerHTML = gfg;
        }
    </script>
</body>
  
</html>


Output:

How to get the current URL using JavaScript ?

How to get the current URL using JavaScript ?

window.location.href: HTML DOM Window.location property returns a Location object that contains information about the current location of the document.

Syntax:

window.location.href

Example 2: This example illustrates to get the current URL of the webpage.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to get the current URL using JavaScript ?
    </title>
</head>
  
<body>
    <h1 style="color:green">
        neveropen
    </h1>
    <h2>
        JavaScript: Program to
        get the website URL ?
    </h2>
    <p>
        <b>
            Click on Below Button
            To Get Current URL
        </b>
    </p>
  
    <button onclick="GetURL()"
        Get URL 
    </button>
    <p id="url"></p>
  
    <script>
        function GetURL() {
            var gfg = window.location.href;
            document.getElementById("url").innerHTML = gfg;
        }
    </script>
</body>
  
</html>


Output:

How to get the current URL using JavaScript ?

How to get the current URL using JavaScript ?

RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS