Monthly Archives: November 2013

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

syntax:

window.location.hash = ‘sourcecodemart’;

2. window.location.host or window.location.hostname

location.host is used to get current domain name.

syntax:

var a = window.location.host;
//window.location.hostname;

3. window.location.href

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

syntax:

var a = window.location.href;
window.location.href = “http://google.com”;

4. window.location.pathname

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

syntax:

var a = window.location.pathname;

5. window.location.search

location.search is used to get url query string.

syntax:

var a = window.location.search;

6. window.location.protocol

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

syntax:

var a = window.location.protocol;

7. window.location.reload

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

syntax:

window.location.reload()

8. window.location.replace

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

syntax:

window.location.replace(‘http://google.com’);

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