Javascript “window.location.href”和“window.location.hash”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10688401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:30:57  来源:igfitidea点击:

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

javascriptjqueryhashhrefwindow.location

提问by kalaba2003

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

我学到了新的“window.location.hash”并在我的jquery代码中尝试而不是“window.location.href”,它们都给出了相同的结果。

Code is here :

代码在这里:

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

What is the difference between them?

它们之间有什么区别?

回答by Selvakumar Arumugam

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

对于像这样的 URL http://[www.example.com]:80/search?q=devmo#test

hashreturn 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.

hash返回 # 符号后面的 URL 部分,包括 # 符号。您可以侦听 hashchange 事件以在支持的浏览器中获得有关哈希更改的通知。

Returns: #test

hrefreturns the entire URL.

href返回整个 URL。

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

Read More

阅读更多

回答by Henrik Karlsson

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

例如测试它 http://stackoverflow.com/#Page

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

回答by Jerome Cance

href is the url

href 是网址

hash is only the anchor after the url

hash 只是 url 之后的锚点

http://www.xxxxxxx.com#anchor

http://www.xxxxxxx.com#anchor

http://www.xxxxxxx.com#anchoris the href

http://www.xxxxxxx.com#anchor是href

"#anchor" is the hash

“#anchor”是哈希值

回答by lonesomeday

hashand hrefare both properties of the window.locationobject. hashis the part of the URL from the #on (or an empty string if there is no #), while hrefis a string representation of the whole URL.

hashhref都是window.location对象的属性。hash是来自#on的 URL 的一部分(如果没有#,则为空字符串),而href是整个 URL 的字符串表示。

回答by DisplayName

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

hash 属性返回 URL 的锚点部分,包括井号 (#)。

回答by VenkataNarendra Bethamcherla

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

这是区别window.location.href和的简单示例window.location.hash

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

对于网址http://www.manm.com/member/#!create

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