JS decomposition URL parameters (object-oriented-minimalist method application) _javascript skills

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.