54

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?

kalaba2003
  • 1,231
  • 3
  • 20
  • 33

6 Answers6

78

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

  • hash - returns 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

informatik01
  • 16,038
  • 10
  • 74
  • 104
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
11

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

href = http://stackoverflow.com/#Page
hash = #Page
Tadeck
  • 132,510
  • 28
  • 152
  • 198
Henrik Karlsson
  • 5,559
  • 4
  • 25
  • 42
4

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

Jerome Cance
  • 8,103
  • 12
  • 53
  • 106
3

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.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
3

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

DisplayName
  • 3,093
  • 5
  • 35
  • 42
3

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
Tunaki
  • 132,869
  • 46
  • 340
  • 423