jquery get Address bar URL parameters (1/2)

Source: Internet
Author: User
Tags extend

For example, your current URL is:
http://www.111cn.net/? test=1&kk=2

If you want to get test, you can introduce the plug-in,
Get it in the following ways:
var test = $.query.get (' test ');

If the parameter has multiple identical names, you can do this:
var arr = $.query.get (' testy ');
Output: [value 1, Value 2, value 3 ...]

If you want to get more than one of the same names, you can do this:
var arrayelement = $.query.get (' testy[1] ');

This plug-in can not only get parameters, but also set parameters.

Set a parameter:
var newurl = $.query.set ("section", 5). ToString ();
Output: "? section=5"

Set two parameters:
var newurl = $.query.set ("section", 5). Set (' Action ', ' do '). ToString ();
Output: "? action=do&section=5"

To delete a parameter:
var oldqueryagain = $.query.remove ("type");

Clear All Parameters:
var emptyquery = $.query.empty ();

Copy all parameters:
var stillthesame = $.query.copy ();


Look at an example

$.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 (querys Tr.indexof ('? ') + 1),
R =/([^&=]+) =? ( [^&]*)/g;
while (e = r.exec (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;
})();

Now let's look at a detailed example

like a url:http://127.0.0.1/index.html?id=999&type=2.
Then you can get the value of ID and type in index.html
var id=$.query.get ("id"); id=999
var type=$.query.get ("type"); type=2

jquery Code

New function (settings) {
var $separator = Settings.separator | | ' & ';
var $spaces = Settings.spaces = = False? False:true;
var $suffix = Settings.suffix = = False? '' : '[]';
var $prefix = Settings.prefix = = False? False:true;
var $hash = $prefix? Settings.hash = = true? "#" : "?" : "";
var $numbers = Settings.numbers = = False? False:true;

Jquery.query = new function () {
The var is = function (o, t) {
Return o!= undefined && o!== null && (!! T? O.constructor = = t:true);
};
var parse = function (path) {
var m, rx =/[([^[]*)]/g, match =/^ ([^[]+) ([. *])? $/.exec (path), base = match[1], tokens = [];
while (M = rx.exec (match[2])) Tokens.push (m[1]);
return [base, tokens];
};
var set = function (target, tokens, value) {
var o, token = Tokens.shift ();
if (typeof target!= ' object ') target = null;
if (token = = "") {
if (!target) target = [];
if (Is (target, array)) {
Target.push (Tokens.length = = 0 Value:set (null, Tokens.slice (0), value));
' Else if (is (Target, object)} {
var i = 0;
while (target[i++]!= null);
Target[--i] = Tokens.length = = 0? Value:set (Target[i], Tokens.slice (0), value);
} else {
target = [];
Target.push (Tokens.length = = 0 Value:set (null, Tokens.slice (0), value));
}
else if (token && token.match (/^s*[0-9]+s*$/)) {
var index = parseint (token, 10);
if (!target) target = [];
Target[index] = Tokens.length = = 0? Value:set (Target[index], Tokens.slice (0), value);
else if (token) {
var index = token.replace (/^s*|s*$/g, "");
if (!target) target = {};
if (Is (target, array)) {
var temp = {};
for (var i = 0; i < target.length; ++i) {
Temp[i] = Target[i];
}
target = temp;
}
Target[index] = Tokens.length = = 0? Value:set (Target[index], Tokens.slice (0), value);
} else {
return value;
}
return target;
};

var queryobject = function (a) {
var self = this;
Self.keys = {};

if (a.queryobject) {
Jquery.each (A.get (), function (key, Val) {
Self.set (Key, Val);
});
} else {
Jquery.each (arguments, function () {
var q = "" + This;
Q = Q.replace (/^[?#]/, "");
Q = Q.replace (/[;&]$/, "");
if ($spaces) q = q.replace (/[+]/g, "");

Jquery.each (Q.split (/[&;;] /), function () {
var key = decodeURIComponent (this.split (' = ') [0] | | "");
var val = decodeuricomponent (this.split (' = ') [1] | | "");

if (!key) return;

if ($numbers) {
if (/^[+-]?[ 0-9]+. [0-9]*$/.test (Val)]
val = parsefloat (val);
else if (/^[+-]?[ 0-9]+$/.test (Val))
val = parseint (Val, 10);
}

val = (!val && val!== 0)? True:val;

if (Val!== false && Val!== true && typeof Val!= ' number ')
val = val;

Self.set (Key, Val);
});
});
}
return self;
};

Queryobject.prototype = {
Queryobject:true,
Has:function (key, type) {
var value = This.get (key);
Return is (value, type);
},
Get:function (key) {
if (!is (key)) return This.keys;
var parsed = Parse (key), base = Parsed[0], tokens = parsed[1];
var target = this.keys[base];
while (target!= null && tokens.length!= 0) {
target = Target[tokens.shift ()];
}
return typeof target = = ' number '? Target:target | | "";
},
Get:function (key) {
var target = This.get (key);
if (Is (target, object)
Return Jquery.extend (True, {}, target);
else if (is (target, array))
Return Target.slice (0);
return target;
},

Home 1 2 last page

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.