Join the DZone community and get the full member experience.Join For Free
If we want to get the full URL of the current web page, we simply have to use
// Shows us the current URL let currentUrl = window.location.href;
let currentDomain = window.location.origin;
For instance, if you were on https://google.com/page/search/go/, the above would simply return https://google.com.
If you want to get only the path after the domain, then run the following:
let currentPath = window.location.pathname;
Although these are the most common uses for
window.location, there are also a number of other useful properties, such as:
- window.location.port - the current port
- window.location.protocol - the current protocol, i.e. https:
- window.location.hash - anything after a hash at the end of the URL
- window.location.hostname - the domain without any ports or protocols
Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.