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

JavascriptJqueryHashHrefwindow.location

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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

href = http://stackoverflow.com/#Page

hash = #Page

Solution 3 - Javascript

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

Solution 4 - Javascript

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.

Solution 5 - Javascript

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

Solution 6 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkalaba2003View Question on Stackoverflow
Solution 1 - JavascriptSelvakumar ArumugamView Answer on Stackoverflow
Solution 2 - JavascriptHenrik KarlssonView Answer on Stackoverflow
Solution 3 - JavascriptJerome CanceView Answer on Stackoverflow
Solution 4 - JavascriptlonesomedayView Answer on Stackoverflow
Solution 5 - JavascriptDisplayNameView Answer on Stackoverflow
Solution 6 - JavascriptVenkataNarendra BethamcherlaView Answer on Stackoverflow