How to get the current URL with JavaScript

published: 08 Jun 2022

2 min read

How to get the current URL with JavaScript

The window.location.href property can be used to get the entire URL of the current web page, including hostname, query string parameters, protocol, and fragment identifier.

const url = window.location.href;

console.log(url);
// https://attacomsian.com/blog/javascript-current-url/

The Location object can also be accessed without the window prefix:

const url = location.href;

Take a look at JavaScript location object tutorial to learn more about the Location object properties and methods.

How to get the current URL with JavaScript | Coding Tips And Tricks

Are we missing something?  Help us improve this article. Reach out to us.

Are you looking for other code tips?

Check out what's on in the category: javascript, programming
Check out what's on in the tag: javascript, programming