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

元气小坏坏 提交于 2019-11-29 04:33:54

问题


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 ok too - 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.

So, to recap:

  1. will all browsers interpret this correctly?

  2. is this in RFC?

  3. can I expect some trouble using this?

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


回答1:


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

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>

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

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.




回答2:


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

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 ActionMethod or whatever is listening with different parameters.



来源:https://stackoverflow.com/questions/7871871/is-link-href-with-just-parameters-starting-with-question-mark-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!