One: the regular expression method
<script type= "Text/javascript" >function getquerystring (URL) { if (URL) { url=url.substr (Url.indexof (" ?") +1); String interception, more efficient than my previous split () method } var result = {},//Create an object to store name, and value queryString =url | | Location.search.substring (1),//location.search Sets or returns the URL (query section) starting with the question mark (?). re =/([^&=]+) = ([^&]*)/g,//regular, specifically not m; while (M = re.exec (queryString)) {//exec () regular expression matches, specifically not result[decodeuricomponent (m[1])] = decodeURIComponent (M [2]); Decodes the encoded URI using decodeURIComponent () } return result; Demo var myparam = getquerystring ("Www.taobao.com?key0=a&key1=b&key2=c");
Note:
1, substr () and substring (start,stop), extracts the character of a string intermediary between two specified subscripts.
Important: Unlike the slice () and substr () methods, substring () does not accept negative parameters.
See http://www.jb51.net/w3school/js/jsref_substring.htm
2, location.search.substring (1), Location.search Sets or returns the URL starting from the question mark (?) (query section).
See http://www.jb51.net/w3school/htmldom/prop_loc_search.htm
3. The exec () method is used to retrieve the matching of regular expressions in a string. It's too powerful to be used.
Reference http://www.jb51.net/w3school/js/jsref_exec_regexp.htm
4. Use decodeURIComponent () to decode the encoded URI.
See http://www.jb51.net/w3school/js/jsref_exec_regexp.htm
II: Split Method (copy on JAVASCRIPT guide)
/* * This function parses ampersand-separated name=value argument pairs from * The query string of the URL. It stores the Name=value pairs in * properties of a object and returns that object. Use it like this: * * var args = Getargs (); Parse args from URL * var q = args.q | | ""; Use argument, if defined, or a default value * var n = args.n? parseint (ARGS.N): 10; */function Getargs () {var args = new Object (); var query = location.search.substring (1); Get query string var pairs = Query.split ("&"); Break at Ampersand for (var i = 0; i < pairs.length; i++) {var pos = pairs[i].indexof (' = '); Look for "Name=value" if (pos = =-1) continue; If not found, skip var argname = pairs[i].substring (0,pos); Extract the name var value = pairs[i].substring (pos+1); Extract the value value = decodeuricomponent (value); Decode it, if NEeded Args[argname] = value; Store as a property} return args; Return the object}
JS decomposition url parameter (regular expression, split comparison) (object-oriented-minimalist method application)