How to get the current URL in JavaScript

in Code Write a comment

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(); // ''

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 // returns the search query

Hi, I'm Renat 👋


If you find this post useful, please let me know in the comments below. 
Renat Galyamov

Want to share this with your friends?

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.

A collection of UI components for
Bootstrap 4/5 and Vue.js

Get now

Write a Comment