The simplest and most straight forward way of getting the URL of the current page is shown below:
But, if we take a sneak peek into the window.location in the developer’s console, it is shown below:
We can observe what it has for us. We could receive a good amount of information from the Window.location object. For example:
If we want to get the protocol only, like HTTP or HTTPS, from the whole URL, we can get that using the very simple window.location.protocol, like the picture below:
And if we want to get the hostname from the URL, we can simply get that using the window.loation.host.
And similarly, if we want to get the pathname only, we can get that using window.location.pathname.
For getting the search query, we can use window.location.search.
So, these are basically some of the ways to get the current URL and extract some specific parts from it according to our requirements. Moreover, window.location has a lot of other options for us.