Your destination for complete Tech news

How to get the current URL in javascript?

27 0
< 1 min read

To get the current URL in JavaScript, you can use the window.location object of the window object. The window.location object contains information about the current URL, and it provides several properties and methods for accessing and manipulating the URL.

Here are some examples of how to use the window.location object to get the current URL in JavaScript:

1. To get the entire URL, you can use the href property:

let currentURL = window.location.href;

This will return the entire URL, including the protocol, domain, path, and query string.

2. To get just the domain, you can use the host property:

let currentDomain = window.location.host;

This will return the domain of the URL, including the subdomain and top-level domain.

3. To get just the path, you can use the pathname property:

let currentPath = window.location.pathname;

This will return the path of the URL, excluding the domain and query string.

4. To get the query string, you can use the search property:

let currentQueryString = window.location.search;

This will return the query string of the URL, including the question mark.

Leave A Reply

Your email address will not be published.

2 × 5 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.