How to Fetch the Current URL in JavaScript?

Estimated read time 1 min read

You can fetch the current URL in JavaScript using the window.location.href property, like this:

const currentUrl = window.location.href;

console.log(currentUrl);
// Output: "https://www.example.com/path/to/page"

The window.location object represents the location of the current webpage, and the href property of this object returns the complete URL of the current page as a string.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply