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:


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


This is anlocationObjects with many properties:


The URL of the current page is displayed in


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 Query string

Download mine for freeJavaScript beginner's manual

More js tutorials: