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.location

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

location

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

The URL of the current page is visible in

location.href

You may also find useful information from other properties:

CodeDescription
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