How to Get the Current URL Using Javascript?


To get the current URL using JavaScript, you can use the window.location.href property.

Here is an example code:

var currentURL = window.location.href;

In this example, the variable currentURL contains the current URL of the page, and the console.log statement logs this URL in the browser console for debugging purposes.

You can also use other properties of the window.location object, such as window.location.protocol,, and window.location.pathname, to get specific parts of the current URL.

For example, to get the domain name of the current URL, you can use

var domainName =;

This will log the domain name of the current URL to the console.

Latest Questions

javascript How to Check if a Javascript Array Includes a Given Value? javascript How to Completely Uninstall Nodejs, and Reinstall From Scratch on Mac OS X? javascript How to Map Values of a Javascript Object to a New Object Using a Function?