This article introduces jquery's method of getting url parameters and url parameters. in the url, add parameters. This article describes jquery's method of getting url parameters in multiple ways, if you are interested in getting a url through jquery and getting url parameters using jquery, this is a frequently-used operation. The following describes how to add code analysis in the form of text instructions, for more information, see the following section.
1. jquery is easy to get the url. the code is as follows:
The code is as follows:
Window. location. href;
In fact, it only uses the basic window object of javascript and does not use jquery knowledge.
2. it is complicated for jquery to obtain url parameters. Regular expressions are needed, so it is important to learn javascript regular expressions well.
First, let's take a look at how javascript is used to obtain a parameter in the url:
// Obtain the parameter function getUrlParam (name) {var reg = new RegExp ("(^ | &)" + name + "= ([^ &] *) in the url. (& | $) "); // Construct a regular expression object var r = window containing the target parameter. location. search. substr (1 ). match (reg); // match the target parameter if (r! = Null) return unescape (r [2]); return null; // return parameter value}
Pass the parameter name in the url through this function to get the parameter value. for example, if the url is
Http: // localhost: 33064/WebForm2.aspx? Reurl = WebForm1.aspx
To obtain the reurl value, you can write it as follows:
The code is as follows:
Var xx = getUrlParam ('reurl ');
After understanding how javascript gets url parameters, we can use this method to expand a method for jquery to get url parameters through jquery. 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 this method is extended for jquery, we can use the following method to obtain the value of a parameter:
The code is as follows:
Var xx = $. getUrlParam ('reurl ');
Complete code: