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">
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      function alertCurrentURL() {
        alert(`Current URL: ${window.location.href}`);
  <button onClick="alertCurrentURL()">Show current URL</button>

The JavaScript code is written inside the