Html 仅带参数(以问号开头)的链接/href 是否有效?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7871871/
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-29 11:13:18  来源:igfitidea点击:

is link / href with just parameters (starting with question mark) valid?

htmlurlparametersget

提问by johndodo

Is this link valid?

这个链接有效吗?

<a href="?lang=en">eng</a>

I know the browsers treat it as expected and I know the empty link would be oktoo - but is it ok to specify just the parameters?

我知道浏览器会按预期处理它,并且我知道空链接也可以- 但是只指定参数可以吗?

I am curious because question mark ("?") is only a convention by most HTTP servers (AFAIK), though I admit it is a prevailing one.

我很好奇,因为问号(“?”)只是大多数 HTTP 服务器(AFAIK)的约定,尽管我承认这是一个普遍的约定。

So, to recap:

所以,回顾一下:

  1. will all browsers interpret this correctly?

  2. is this in RFC?

  3. can I expect some trouble using this?

  1. 所有浏览器都会正确解释吗?

  2. 这是在 RFC 中吗?

  3. 我可以期待使用它会遇到一些麻烦吗?

UPDATE:the intended action on click is to redirect to the same page, but with different GET parameters ("lang=en" in above example).

更新:点击时的预期操作是重定向到同一页面,但使用不同的 GET 参数(上例中的“lang=en”)。

采纳答案by Kobi

Yes, it is.
You can find it in RFC 1808 - Relative Uniform Resource Locators:

是的。
您可以在RFC 1808 - 相对统一资源定位器中找到它:

Within an object with a well-defined base URL of
Base: <URL:http://a/b/c/d;p?q#f>
the relative URLs would be resolved as follows:

5.1. Normal Examples

?y= <URL:http://a/b/c/d;p?y>

在具有明确定义的
Base URL 的对象中:<URL:http://a/b/c/d;p?q#f>
相对 URL 将按如下方式解析:

5.1. 正常例子

?y= <URL:http://a/b/c/d;p?y>

RFC 3986 - Uniform Resource Identifier (URI): Generic Syntaxrestates the same, and adds more details, including the grammar:

RFC 3986 - 统一资源标识符 (URI):通用语法重申相同,并添加了更多细节,包括语法:

relative-ref  = relative-part [ "?" query ] [ "#" fragment ]

relative-part = "//" authority path-abempty
             / path-absolute
             / path-noscheme
             / path-empty     #; zero characters

Now, that is not to say all browsers implement it according to the standard, but it looks like this should be safe.

现在,并不是说所有浏览器都按照标准实现它,但看起来这应该是安全的。

回答by Matas Vaitkevicius

Yes - and it will hit the current url with parameters what you are passing.

是的 - 它将使用您传递的参数访问当前 url。

It is very convenient to use in situations where you want to make sure you do not cross current page/form boundary and keep on hitting same ActionMethodor whatever is listening with different parameters.

在您想确保不跨越当前页面/表单边界并继续ActionMethod使用不同参数进行相同或任何监听的情况下,使用它非常方便。