When it comes to working with URLs in JavaScript, there are a few different ways to get the current URL that you are on. In this article, we will go over some of the different methods that you can use to get the current URL in JavaScript.

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 document.URL property and then use one of JavaScript’s built-in string methods to extract just the pathname from the full URL string.

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 window.location.protocol or window.location.hostname.


There are 2 ways to get the current URL from a browser using JavaScript. The main one is window.location.href and 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 document.URL