URL、hash 或 querystring 中应该先出现什么?

What should come first in URL, hash or querystring?

一些在线文章说,querystringhash 在 URL 中没有标准,但我们正在关注继续发生的事情。所以,我的问题是在同一个 URL.

中同时拥有查询字符串和散列的更好方法应该是什么?

我认为的问题是,如果hash跟在querystring后面,它可以成为一些querystring数据的值,如果querystring跟在hash后面,整个querystring可能会变成一个hask。那么,我应该遵循什么顺序呢?

这是我在 JavaScript 中尝试过的:

window.location="alpha#abc?def=34";
console.log(window.location);

结果是:

Location {
  hash: "#abc?def=34",
  search: "",
  ...otherData
}

然后:

window.location="alpha?abc=34#def";
console.log(window.location);

结果是:

Location {
  hash: "#def",
  search: "?abc=34",
  ...otherData
}

很明显,JavaScript 不区分# 符号后的任何内容,而 hash 之前的查询字符串工作正常。

So, we should use querystring first and then hash.

Some online articles says, that there is no standard for querystring and hash in URL

要么他们错了,要么你误解了他们。

查询字符串必须出现在片段标识符(您称之为散列)之前。

specification 显示 URI 的格式:

URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]

它清楚地显示了查询后出现的片段。

if hash follows querystring, it can become a value to some querystring data

不能。 # 是一个特殊字符,表示片段的开始。要在查询字符串数据中包含一个,需要将其转义为 %23.