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.
Are we missing something? Help us improve this article. Reach out to us.
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.
Are you looking for other code tips?
JS Nooby
Javascript connoisseur