How to get current URL with JavaScript?

window.location.href JavaScript provides you with different methods to get and set the current URL , which is displayed in the web-browser's address bar. All these methods use the Location object , which is a property of the Window object. Use its properties to get information on the current page address or use its methods to do some page redirect or refresh.
run this source code Browser View

URL of this page is:

Get current url | Full Source | javascript
<!DOCTYPE html> <html> <body> <p id="href">URL of this page is:</p> <script type="text/javascript"> function getURL() { document.getElementById("href").innerHTML = "URL of this page is:<br>" + window.location.href; } </script> <form id="b"> <input type="button" id="two" onclick="getURL()" value="Get URL of this page" /> </form> </body> </html>

URL structure

URL's are also called links. It is an address on the internet. It's made up of a protocol , domain name, and a path.
<protocol>//<hostname>:<port>/<pathname><search><hash>

Get Current URL and Components with JavaScript (Protocol, Domain, Port, Path, Query, Hash)

JavaScript window.location properties

Using these window.location object properties you can access all of these URL components and what they can set or return:
  1. href - Full URL (http://www.example.com:8082/index.php#tab2)
  2. protocol - Protocol Schema (http: or https)
  3. host - Domain name + port (www.example.com:8082)
  4. hostname - Domain name (www.example.com)
  5. port - Port Number (8082)
  6. pathname - the path name of the URL (index.php)
  7. search - ? followed by Query String (?foo=789)
  8. hash - # followed by the Anchor or Fragment identifier (#tab2)
run this source code Browser View

window.location properties in javascript

window.location properties| FullSource | JavaScript
<!DOCTYPE html> <html> <body> <h2>window.location properties in javascript</h2> <p id="href"></p> <p id="protocol"></p> <p id="host"></p> <p id="hostname"></p> <p id="port"></p> <p id="pathname"></p> <p id="search"></p> <p id="hash"></p> <script type="text/javascript"> function getURLProperties() { document.getElementById("href").innerHTML = "URL of this page is:<br>" + window.location.href; document.getElementById("protocol").innerHTML = "Protocol of this page is:<br>" + window.location.protocol; document.getElementById("host").innerHTML = "Host of this page is:<br>" + window.location.host; document.getElementById("hostname").innerHTML = "Hostname of this page is:<br>" + window.location.hostname; document.getElementById("port").innerHTML = "Port of this page is:<br>" + window.location.port; document.getElementById("pathname").innerHTML = "Pathname of this page is:<br>" + window.location.pathname; document.getElementById("search").innerHTML = "Search of this page is:<br>" + window.location.search; document.getElementById("hash").innerHTML = "Hash of this page is:<br>" + window.location.hash; } </script> <form id="d"> <input type="button" id="four" onclick="getURLProperties()" value="Get URL Properties of this page" /> </form> </body> </html>