How to get current URL Location using Javascript HTML


Ever came across a requirement where you need to grab the current page URL from JavaScript. See below examples to learn how to achieve this and some other parts of URL with the help of window.location object. You can either use the code directly or save it in a variable and then use.

Code Type : Get entire Location (URL)
var url = window.location.href;
Code Type : Get Location HostName
var hostName = window.location.hostname;






Author Info:

Rakesh (He/Him) has a Masters Degree in Computer Science with over 15+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright Code2care© 2024 | Privacy Policy | About Us | Contact Us | Search