javascript - Conventions for querystring parameters -
when have url this:
http://server/site?firstname=jack&lastname=daniels
i understand in javascript query firstname should homecoming "jack".
but should query firstname homecoming in next cases:
http://server/site?lastname=daniels http://server/site?firstname&lastname=daniels http://server/site?firstname=&lastname=daniels
[edit] reply of comments: of above legal querystrings, question not how retrieve parameters how interpret them.
for record, parse querystrings next regular look covers cases:
/([^?=&;]+)(?:=([^&;]*))?/g
apparently there's a popular question on how retrieve querystring parameters, reply wrong (or @ to the lowest degree not addressing border cases).
[update] selection based on answers @bergi , @zzzzbov:
http://server/site?lastname=daniels => firstname: undefined http://server/site?firstname&lastname=daniels => firstname: true http://server/site?firstname=&lastname=daniels => firstname: "" http://server/site?firstname=jack&lastname=daniels => firstname: "jack"
a side effect had modify regex, above rules =
sign needs captured:
/([^?=&;]+)(=[^&;]*)?/g
but should query return
that's decision, need? there several querystring-evaluating functions of different complexity. of them can deal repeated, nested, bracket-syntax values, others not. famous ones can found in how can query string values? , duplicate questions.
however, asked conventions:
http://server/site?lastname=daniels
the result should falsy represent absence of firstname
. might take null
or undefined
.
http://server/site?firstname&lastname=daniels
this represents boolean parameter, returning true
legitimate. yet i'm sure there libs ignore format.
http://server/site?firstname=&lastname=daniels
that's quite empty string ""
.
javascript html url query-string
No comments:
Post a Comment