是"?&"对于网址

时间:2015-07-09 02:26:12

标签: url browser

在URL的查询字符串中省略第一个参数是否有效?

http://www.asdf.com/?&foo=bar&foo2=bar2

如果我错了,请纠正我,但这意味着一个空参数,然后是" foo"和" foo2"参数。

Chrome和Firefox似乎正在正确地解释查询参数,即使第一个被遗漏了,但我想确保这实际上有效吗?

1 个答案:

答案 0 :(得分:0)

引自RFC 3986

3.  Syntax Components

   The generic URI syntax consists of a hierarchical sequence of
   components referred to as the scheme, authority, path, query, and
   fragment.

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

...

3.4.  Query

   The query component contains non-hierarchical data that, along with
   data in the path component (Section 3.3), serves to identify a
   resource within the scope of the URI's scheme and naming authority
   (if any).  The query component is indicated by the first question
   mark ("?") character and terminated by a number sign ("#") character
   or by the end of the URI.

因此,网址http://www.asdf.com/?&foo=bar&foo2=bar2被细分为:

[http] ":" [//www.asdf.com/] "?" [&foo=bar&foo2=bar2]

根据谁为您解析查询字符串,他们将能够识别出第一个&无效。这是一个示例解析器。

var query_string = "&foo=bar&foo2=bar2";
query_string.split("&").forEach(function(value) {    
     console.log(value.split("=")); 
 });

输出:

[""]
["foo", "bar"]
["foo2", "bar2"]

键值对将需要键和值。您可以通过检查数组是否为长度=== 2来判断它们是否有效。