Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to get the current URL using JavaScript ?

  • Last Updated : 19 Nov, 2021

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://).

Syntax:

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

document.URL

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

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

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
    h1 {
        color: green;
    }
      
    h2 {
        font-family: Impact;
    }
      
    body {
        text-align: center;
    }
    </style>
</head>
  
<body>
    <h1>GeeksforGeeks</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:

Getting the current website URL




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!