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: