How to get the current URL using JavaScript ?

The task is to get the Website URL. 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.

Syntax:

document.URL

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:
Before clicking the button:

After clicking the button:

Supported Browser are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.