Anjan Dutta

How to get the current URL with javascript

How to get the current URL with javascript

We can get the current URL using the window.location.href.

See the example code below.

let currentUrl = window.location.href;
console.log(currentUrl);

The window.location object can get us some more URL properties.

  • window.location.href - to get the URL of the current page
  • window.location.hostname - to get the domain name of the web host
  • window.location.pathname - to get the path and filename of the current page
  • window.location.protocol - to get the web protocol of the current page (HTTP: or HTTPS:)