jquery get URL parameter plug-in instance code

Source: Internet
Author: User
Tags php tutorial

jquery Get URL Address

Jquery.fn.getparmbyurl = function (o) {
var url = window.location.tostring ();
var tmp;
if (URL && url.indexof ("?")) {
var arr = Url.split ("?");
var parms = arr[1];
if (parms && parms.indexof ("&")) {
var parmlist = Parms.split ("&");
Jquery.each (Parmlist,function (key,val) {
if (Val && val.indexof ("=")) {
var Parmarr = val.split ("=");
if (o) {
if (typeof (o) = = "string" && o = = Parmarr[0]) {
TMP = parmarr[1] = = null? ':p armarr[1];
}
}
else{
TMP = parms;
}
}
});
}
}
return TMP;

Application method

$ (function () {
$ (). Getparmbyurl () = = Null?window.location.href = '? id=100&class=4&sh=y&city= maoming &action ': ';
$ ("#btn"). Click (function () {
var a = $ (). Getparmbyurl ($ ("#key"). Val ());
alert (a);
});
});


Web effects get URL parameters and addresses

function getquerystring (name)
{
var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)");
var r = window.location.search.substr (1). Match (REG);
if (r!=null) return unescape (r[2]); return null;
}

The URL's parameter & parameter name 1=xxxx& parameter name 2=xxxx& parameter name 3=xxxx

Alert (getquerystring ("parameter name 1"));
Alert (getquerystring ("parameter Name 2"));
Alert (getquerystring ("parameter name 3"));

Detailed jquery get URL parameter description

Your current URL is:
http://www.xxx.com/index.php tutorial? 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 ();

Plugin Address:
Http://plugins.jquery.com/project/query-object

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.