How to get current URL Location using Javascript HTML

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

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

This is not an AI-generated article but is demonstrated by a human.

Please support independent contributors like Code2care by donating a coffee.

Buy me a coffee!

Buy Code2care a Coffee!

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!