Javascript/jquery method to get address bar URL parameters

Source: Internet
Author: User
Tags regular expression

  This article is mainly on the javascript/jquery get address bar URL parameters of the method is introduced, the need for friends can come to the reference, I hope to help you.

Using jquery to get URLs and using jquery to get URL parameters is the operation we often use   1, jquery get URL is very simple, code as follows   code as follows: Window.location.href;     Actually just use JavaScript to base the Window object, and do not use jquery knowledge   2, jquery get URL parameters more complex, to use regular expressions, So it's important to learn JavaScript's regular style   first look at how simple JavaScript is to get a parameter   code in a URL: function geturlparam (name) {var reg = NE W RegExp ("(^|&)" + name + "= ([^&]*) (&|$)"); Constructs a regular expression object containing the target parameter var r = Window.location.search.substr (1). Match (REG);  //matching 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 value of the parameter, for example, the URL is http://www.xxx.loc/admin/write-post.php?cid=79 we're going to get the CID value, You can write this: the code is as follows: Geturlparam (' CID ');   Understand the way JavaScript gets URL parameters, we can use this method to extend a method for jquery to get URL parameters through jquery, and the following   code expands a Geturlparam () method for jquery The     code is as follows: (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);   Extended this method for jquery, we can then get the value of a parameter by using the following method   code as follows: $.geturlparam (' CID ');  
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.