What is the difference between “[removed].href” an

2019-03-10 16:00发布

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?

7条回答
Fickle 薄情
2楼-- · 2019-03-10 16:02

One important difference is that window.location.hash is urldecoded, while the fragment identifier in window.location.href is not urldecoded. In other words:

window.location.hash.split('#')[1] != window.location.href.split('#')[1]

If the URL is http://example.com/page#3%3D3 then:

window.location.hash.split('#')[1] == '3=3'
window.location.href.split('#')[1] == '3%3D3'

In other words:

window.location.hash.split('#')[1] == decodeURIComponent(window.location.href.split('#')[1])
查看更多
倾城 Initia
3楼-- · 2019-03-10 16:07

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

href = http://stackoverflow.com/#Page
hash = #Page
查看更多
【Aperson】
4楼-- · 2019-03-10 16:16

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
查看更多
Ridiculous、
5楼-- · 2019-03-10 16:23

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

查看更多
家丑人穷心不美
6楼-- · 2019-03-10 16:27

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

查看更多
虎瘦雄心在
7楼-- · 2019-03-10 16:29

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

查看更多
登录 后发表回答