JQuery obtains the address bar parameter plug-in (imitating c #) _ jquery

Source: Internet
Author: User
Some time ago, I wrote a plug-in to get the address bar parameters like. net's Requst. QuerySting [& quot; param & quot. Now let's share it with you. I think this plug-in is quite useful. The Code is as follows:


$. Request = (function (){
Var apiMap = {};
Function request (queryStr ){
Var api = {};
If (apiMap [queryStr]) {
Return apiMap [queryStr];
}
Api. queryString = (function (){
Var urlParams = {};
Var e,
D = function (s) {return decodeURIComponent (s. replace (/\ +/g ,""));},
Q = queryStr. substring (queryStr. indexOf ('? ') + 1 ),
R =/([^ & =] +) =? ([^ &] *)/G;
While (e = r.exe c (q ))
UrlParams [d (e [1])] = d (e [2]);

Return urlParams;
})();
Api. getUrl = function (){
Var url = queryStr. substring (0, queryStr. indexOf ('? ') + 1 );
For (var p in api. queryString ){
Url + = p + '=' + api. queryString [p] + "&";
}
If (url. lastIndexOf ('&') = url. length-1 ){
Return url. substring (0, url. lastIndexOf ('&'));
}
Return url;
}
ApiMap [queryStr] = api;
Return api;
}
$. Extend (request, request (window. location. href ));
Return request;
})();


Next, we will briefly introduce the plug-in usage.
You can use $. request. queryString ["key"] to obtain the key parameter in the address bar.
In most cases, I use this plug-in to read some configuration information.
We sometimes add some other non-html attributes to the HTML tag as the tag information. For example, we sometimes write the Demo
In this way, we use
Var config = $. request ($ ("# demo"). attr ("config"). queryString;
Var title = config. title;
It is easy to read the configuration information above a, and we do not need to add too many other non-Html attributes to.

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.