The Things you can do using window.location object

In today’s era JavaScript uses more n more in web apps. There are many things you can do with javascript like reload page, replace url, get useragent, url string, url address, etc… Here I am going to explain window.location object. It is more userful if you are playing with JavaScript.

1. window.location.hash

location.hash is used for jump on anchor name whereever you defined in a page. eg. Jump Here


window.location.hash = ‘sourcecodemart’;

2. or window.location.hostname is used to get current domain name.


var a =;

3. window.location.href

location.href is used to get whole current url. Additionaly you can also redirect user to another page.


var a = window.location.href;
window.location.href = “”;

4. window.location.pathname

location.pathname is used to get current page that you are using.


var a = window.location.pathname;

5. is used to get url query string.


var a =;

6. window.location.protocol

location.protocol is to get url protocol like http: or https:.


var a = window.location.protocol;

7. window.location.reload

location.reload() function is used to reload current page.



8. window.location.replace

location.replace function is used to redirect user to another location.



Here I explained very brief introduction about window.location object. If I missed anything then please comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *