
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
bylocation.href
property - Current domain name:
codingtip.blogspot.com
bylocation.hostname
property - Current path name:
/2014/01/get-url-domain-name-path-name-javascript.html
bylocation.pathname
property - Current port:
by
location.port
property - Current protocol:
http:
bylocation.protocol
property
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;