How to get the current URL in JavaScript

JavaScript is used to open the browser and get the URL. You can also rely on JavaScript’s  location  property on the  window  object.


 window  is the global object of the browser. The property can be referenced by


This is a Location object that has many properties of its own.

The URL of the current page is visible in


You may also find useful information from other properties:

location.hostnamethe host name
location.originthe origin
location.hashthe hash, the part that follow the hash # symbol
location.pathnamethe path
location.portthe port
location.protocolthe protocol
location.searchthe query string