Before modification:
Copy Code code as follows:
<script type= "Text/javascript" >
var url= "Www.taobao.com?key0=a&key1=b&key2=c";
function parsequerystring (URL) {
var str=url.split ("?") [1],
Items=str.split ("&");
var Arr,name,value;
for (var i = 0, L = items.length i < l; i++) {
Arr=items[i].split ("=");
Name= Arr[0];
Value= Arr[1];
This[name]=value;
}
}
var obj=new parsequerystring (URL);
Alert (Obj.key1)
</script>
After modification:
Copy Code code as follows:
<script type= "Text/javascript" >
var url= "Www.taobao.com?key0=a&key1=b&key2=c";
var pqstring={
Createnew:function (URL) {
var str=url.split ("?") [1],
Items=str.split ("&");
var Arr,name,value;
for (var i = 0, L = items.length i < l; i++) {
Arr=items[i].split ("=");
Name= Arr[0];
Value= Arr[1];
This[name]=value;
}
}
}
var obj=new pqstring.createnew (URL);
Alert (Obj.key1)
</script>
Thanks to the grassroots program ape is very powerful to rewrite this method, more rigorous, efficient, the last side in some more to learn!!!
Copy Code code as follows:
<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 for saving name, and value
QueryString =url | | Location.search.substring (1),//location.search Sets or returns the URL (part of the query) starting from the question mark (?).
Re =/([^&=]+) = ([^&]*)/g,//regular, specific
M
while (M = Re.exec (querystring)) {//exec () a matching of regular expressions, specifically
Result[decodeuricomponent (m[1])] = decodeURIComponent (m[2]); Decoding the encoded URI using decodeURIComponent ()
}
return result;
}
Demo
var myparam = getquerystring ("Www.taobao.com?key0=a&key1=b&key2=c");
alert (myparam.key1);
</script>
Note:
1, substr () and substring (start,stop), extracts the character string intermediary between the two specified subscript.
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 (part of the query) starting from the question mark (?).
See http://www.jb51.net/w3school/htmldom/prop_loc_search.htm
3. The EXEC () method retrieves 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