Copy codeThe 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 <a id = "demo" href = "javascript :; "config = "? Title = hello & auto = true & "> Demo </a>
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.