javascript URL 中的多个井号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10850781/
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
Multiple hash signs in URL
提问by
Or maybe you call it "sharp" - the # symbol.
或者也许你称之为“锋利”——# 符号。
I've came across one instance, where #! and # used simultaneously in a single URL. From reading other articles, including RFC, I can't understand whether that is a legal combination or not. When encountering such page Mozilla browser (Iceweasel in this case) displays the URL as having 2 #'s, while Chrome displays only one, but dies shortly afterwards (the tab containing the page becomes unresponsive and crashes - but it may not be connected).
我遇到过一个实例,其中 #! 和 # 在单个 URL 中同时使用。通过阅读其他文章,包括 RFC,我无法理解这是否是一个合法的组合。当遇到这样的页面时,Mozilla 浏览器(在本例中为 Iceweasel)将 URL 显示为有 2 个 #,而 Chrome 只显示一个,但不久之后就消失了(包含该页面的选项卡变得无响应并崩溃 - 但它可能无法连接) .
Now, my question is, is it legal to have both in one URL, is it maybe legal and redundant (should be normalized), or is it just a bug in Mozilla browser? So, suppose I'm making an AJAX request, or trying to navigate the browser history - what should I do, if I encounter this situation?
现在,我的问题是,在一个 URL 中同时拥有两者是否合法,它是否合法且多余(应该规范化),或者它只是 Mozilla 浏览器中的一个错误?因此,假设我正在发出 AJAX 请求,或尝试浏览浏览器历史记录 - 如果遇到这种情况,我该怎么办?
RFC-3986: http://tools.ietf.org/html/rfc3986#section-3.4, which should be clarifying it... just in case.
RFC-3986:http: //tools.ietf.org/html/rfc3986#section-3.4,这应该澄清它......以防万一。
Also: https://developers.google.com/webmasters/ajax-crawling/docs/specificationhow Google crawlers see things.
另外:https: //developers.google.com/webmasters/ajax-crawling/docs/specificationGoogle 爬虫如何看待事物。
采纳答案by apsillers
The format for a fragment only allows slashes, question marks, and pchar
s. If you look up the RFC, you'll see that the hash mark is not a valid pchar
.
片段的格式只允许使用斜杠、问号和pchar
s。如果您查看 RFC,您会发现哈希标记不是有效的pchar
.
However, browsers will try their best to read non-valid URLs by treating repeat hashes as though they are escaped, as you can see by checking the value of window.location.hash
(in IE, Firefox, and Chrome) for
但是,浏览器会尽量读取无效的 URL,方法是将重复哈希视为已转义,正如您可以通过检查window.location.hash
(在 IE、Firefox 和 Chrome 中)的值看到的
http://www.example.com/hey#foo#bar
which is the same window.location.hash
for
这是相同window.location.hash
的
http://www.example.com/hey#foo%23bar
回答by nebulousGirl
It may be legal as @apsillers mentioned. But I would avoid it unless necessary as it can cause a certain confusion concerning the url.
正如@apsillers 提到的那样,它可能是合法的。但除非必要,否则我会避免使用它,因为它可能会导致对 url 的某些混淆。
That kind of url:
那种网址:
http://www.example.com/hey#foo#bar
Seems really confusing to me and will be even more confusing to regular users and maybe search engines.
对我来说似乎真的很困惑,对普通用户和搜索引擎来说甚至会更加困惑。