What is the difference between "window.location.href" and "window.location.hash"?

kalaba2003 picture kalaba2003 · May 21, 2012 · Viewed 70.4k times · Source

I learned "window.location.hash" new and tried in my jquery code instead of "window.location.href" and both of them gave same results.

Code is here :

window.location.href = ($(e.currentTarget).attr("href"));
window.location.hash = ($(e.currentTarget).attr("href"));

What is the difference between them?

Answer

Selvakumar Arumugam picture Selvakumar Arumugam · May 21, 2012

For an URL like http://[www.example.com]:80/search?q=devmo#test

hash return the part of the URL that follows the # symbol, including the # symbol. You can listen for the hashchange event to get notified of changes to the hash in supporting browsers.

Returns: #test

href returns the entire URL.

Returns: http://[www.example.com]:80/search?q=devmo#test

Read More