Using jquery to get URLs and using jquery to get URL parameters is an operation we often use
1, jquery get URL is very simple, the code is as follows:
actually just use JavaScript to base the Window object, and it doesn't work jquery of knowledge.
2, jquery get URL parameters more complex, to use regular expressions, so learn JavaScript regular style how important things
First look at how the simple JavaScript is going to get a parameter in the URL:
Gets the parameter
function Geturlparam (name) {
var reg = new RegExp (^|&) + name + = ([^&]*) (&|$)) in the URL; Constructs a regular expression object containing the target parameter
var r = window.location.search.substr (1). Match (reg);//Match target parameter
if (r!= null) return Unescape (r[2]); return null; Return parameter Value
}
Passing the parameter name in the URL through this function can get the value of the parameter, such as the URL
Http://localhost:33064/WebForm2.aspx?reurl=WebForm1.aspx
We're going to get the value of Reurl, and we can write this:
var xx = Geturlparam (' Reurl ');
Understanding the way JavaScript gets URL parameters, we can extend a method for jquery to get URL parameters through jquery, and the following code expands a Geturlparam () method for jquery
(function ($) {
$.geturlparam = function (name) {
var reg = new RegExp (^|&) "+ name +" = ([^&]*) (&|$) ");
var r = window.location.search.substr (1). Match (reg);
if (r!= null) return unescape (r[2]); return null;
}
}) (JQuery);
After extending this method for jquery, we can get the value of a parameter by using the following method:
var xx = $.geturlparam (' Reurl ');
Complete code:
<script src= "Js/jquery-1.7.2.min.js" type= "Text/javascript" ></script> <script type= "Text/javascript" > $ (function () {//Method Two: (function ($) {$.geturlparam = Func
tion (name) {var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)");
var r = window.location.search.substr (1). Match (REG); if (r!= null) return unescape (r[2]);
return null;
}) (JQuery);
Method Two: var xx = $.geturlparam (' Reurl ');
Method One://var xx = Geturlparam (' Reurl ');
Alert (XX);
}); Method One://Get Parameter function Geturlparam (name) {var reg = new RegExp (^|&) + name + "= ([^&]*)" in URL ;|$)"); Constructs a regular expression object containing the target parameter var r = Window.location.search.substr (1). Match (REG); Match the target parameter if (r!= null) return unescape (r[2]); return null; Returns the parameter value} </script>
2014-4-23 modified
Today in the above method to get the parameters in the URL, the URL passed in the Chinese parameters in the parsing of the time no matter how the test, get the garbled. After some debugging found that I passed the parameters, the use of encoding is encodeURI, and the above method in the resolution of the parameters of the code used is unescape, modified to decodeURI can be.
Attached: W3school in the introduction:
JavaScript unescape () function
Definitions and usage
The unescape () function decodes a string encoded by escape ().
Parameters |
Description |
String |
Necessary. The string to decode or reverse. |
Description
This function works by finding a sequence of characters in the form%xx and%uxxxx (x represents a hexadecimal number), which is decoded by replacing such sequences with Unicode characters \u00xx and \uxxxx.
Tips and comments
Note: ECMAScript v3 has removed the unescape () function from the standard and objected to its use, so you should use decodeURI () and
decodeURIComponent () instead.
In summary: JavaScript is consistent with the parameter encoding and decoding method:
Escape () unescape ()
encodeURI () decodeURI ()
encodeURIComponent () decodeuricomponent ()
Find another kind of javascript on the Internet method to get the parameters in the URL:
<script language= "JavaScript" type= "text/javascript" >
function geturlparms ()
{
var args=new Object ();
var query=location.search.substring (1);//Get query string
var pairs=query.split ("&")//Disconnect for
(var i=0;i at Comma) <pairs.length;i++)
{
var pos=pairs[i].indexof (' = ');//Find Name=value
if (pos==-1) continue;// Skip Var argname=pairs[i].substring (0,pos) If it is not found;/
/extract name
var value=pairs[i].substring (pos+1);//Extract value
Args[argname]=unescape (value);//Save As Property
}
return args;
}
var args = new Object ();
args = Geturlparms ();
If you want to find the parameter key: if
(args["id"]!=undefined)
{
//If you want to find the parameter key:
var value1 = args["id"];
alert (value1);
} </script>
The above use of jquery to get URL and URL parameters of a simple example is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.