Mohan pd.
Mohan pd. Author of The Coders Blog. Also a professional fullstack javascript developer working with various tech company and client around world.

Best way to get the full URL or query string of current page in JavaScript.

Feb 24, 2019 · 1 min
Best way to get the full URL or query string of current page in JavaScript.

Handling URL in javascript can be easy if you know following methods.

You can save a picture for quick reference

1. You can get the full URL of the current page using the window.location.href.property.

2. You can get the domain name from the URL at current page using the window.location.hostname.property

3. You can get the pathname from the current URL a visitor is on using the window.location.pathname.property.

4. You can get the query string from the current URL using the window.location.search.property. You can further get the value or individual parameters using the serachParams property on the URL object.

5. You can get the hash or fragment identifier from URL using the window.location.hash.property.