How to get the current URL in JavaScript

Find out what JavaScript provides you to get the current URL opened in the browser.

To get the current URL of the page opened in the browser using JavaScript, you can rely onlocationBrowser atwindowpurpose:

window.location

sincewindowIs a global object in the browser, this attribute can be referenced as

location

This is anlocationObjects with many properties:

window.location

The URL of the current page is displayed in

location.href

Other characteristicslocationProvide useful information:

Code description
location.hostname CPU name
location.origin origin
location.hash The number sign, the part after the # sign
location.pathname path
location.port port
location.protocol protocol
location.search Query string

Download mine for freeJavaScript beginner's manual


More js tutorials: