Jquery method for getting url parameters and adding parameters to URLs _ jquery

Source: Internet
Author: User
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:

 
Related Article

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.