The first method that we will look at is using the
window.location object. It can be used to get a variety of information about the current URL that you are on. To get the current URL, you can use the
window.location.href property. This property will give you the full URL of the page that you are currently on including any query parameters that may be in the URL.
Another way to get the current URL is by using the
document.URL property. This property gives you the same information as the
window.location.href property, but it is a bit more browser-compatible since it is part of the Document Object Model (DOM).
If you need to get just the pathname of the current URL, you can use the window.location.pathname property or the
Lastly, if you need to get information about specific parts of the current URL (such as protocol, hostname, port, etc.), you can use various properties of the
window.location object such as
document.URL is an alternative option.
To check that both approaches are functional, you can use a simple
alert to make show current browser URL with JS.
You can do the same for