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:

Code Description
location.hostname the host name
location.origin the origin
location.hash the hash, the part that follow the hash # symbol
location.pathname the path
location.port the port
location.protocol the protocol
location.search the query string

 

Exit mobile version