JavaScript program to get the current URL of the browser

To get the current URL in JavaScript, we can use use window.location.href method. It returns the current opened URL.

Example program :

Create one index.html file and put the below code in it :

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Sample</title>
  </head>
  <body>
    <script>
      function alertCurrentURL() {
        alert(`Current URL: ${window.location.href}`);
      }
    </script>
  </body>
  <button onClick="alertCurrentURL()">Show current URL</button>
</html>

The JavaScript code is written inside the