Get the current URL with JavaScript?

Created by VolkerKLinked to 4.5m issues across 51 teams

tl;dr

The following example uses the window.location object to get the current URL in JavaScript:

var currentURL = window.location.href; console.log(currentURL);

The window.location object contains information about the current URL, such as the protocol (http: or https:), hostname (example.com), pathname (/path/to/page), and more. In the example above, the href property is used to get the full URL. The result is stored in the currentURL variable and logged to the console.