[Solved] What is the difference between “window.location.href” and “window.location.hash”?

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?

Solution #1:

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

Solution #2:

Test it on for example http://stackoverflow.com/#Page

href = http://stackoverflow.com/#Page
hash = #Page
Respondent: Henrik Karlsson

Solution #3:

href is the url

hash is only the anchor after the url

http://www.xxxxxxx.com#anchor

http://www.xxxxxxx.com#anchor is the href

“#anchor” is the hash

Respondent: Jerome Cance

Solution #4:

hash and href are both properties of the window.location object. hash is the part of the URL from the # on (or an empty string if there is no #), while href is a string representation of the whole URL.

Respondent: lonesomeday

Solution #5:

The hash property returns the anchor portion of a URL, including the hash sign (#).

Respondent: DisplayName

Solution #6:

Here is the simple example for difference between window.location.href and window.location.hash

For the URL http://www.manm.com/member/#!create:

  • href: http://www.manam.com/member/#!create
  • hash: #!create

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.