In this tutorial, you’ll learn how to get the current page URL. We’ll be using the window.location
object.
const currentURL = () => window.location.href;
currentURL(); // 'https://renatello.com/javascript-current-url/'
You can also use other window.location
properties to create a custom URL:
window.location.hostname // returns the domain name of the web host
window.location.pathname // returns the path and filename of the current page
window.location.protocol // returns the web protocol used (http: or https:)
window.location.assign // loads a new document
window.location.search // returns the search query
If you find this post useful, please let me know in the comments below.
Cheers,
Renat Galyamov
Want to share this with your friends?
👉renatello.com/javascript-current-url
PS: Make sure you check other JavaScript tutorials, e.g. how to separate numbers with commas in JavaScript or get the day of the year in JavaScript.