Using window.location object, we can get current page address informations including url, domain name of web server, path name, protocol and port in Javascript.

Notes:The window.location object can be written without the window prefix.
For example, we try to get current page information:
  • URL of current page:  http://codingtip.blogspot.com/2014/01/get-url-domain-name-path-name-javascript.html by location.href property
  • Current domain name: codingtip.blogspot.com by location.hostname property
  • Current path name: /2014/01/get-url-domain-name-path-name-javascript.html by location.pathname property
  • Current port: by location.port property
  • Current protocol: http: by location.protocol property
Reference source code if you want to try by yourself:

HTML source:
<code class="code" id="current_url"></code> 
<code  id="current_domain"></code>
<code  id="current_path"></code>
<code  id="current_port"></code>
<code  id="current_protocol"></code>
Javascript source:
document.getElementById('current_url').innerHTML= window.location.href;
document.getElementById('current_domain').innerHTML= window.location.hostname;
document.getElementById('current_path').innerHTML= window.location.pathname;
document.getElementById('current_port').innerHTML= window.location.port;
document.getElementById('current_protocol').innerHTML= window.location.protocol;