Use jquery obtain url And the use of jquery obtain url Parameters are operations that we often use

1、jquery obtain url It's simple , The code is as follows :

window.location.href;

Actually, it's just using javascript Based on window object , It's no use jquery Knowledge .

2、jquery obtain url The parameters are complicated , You need regular expressions , So learn well javascript What an important thing regular forms are

First of all, let's look at simply passing through javascript How to get it url A parameter in :

 // obtain url Parameters in 
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // Construct a regular expression object with target parameters
var r = window.location.search.substr(1).match(reg); // Match target parameters
if (r != null) return unescape(r[2]); return null; // Return parameter value
}

Pass... Through this function url You can get the value of the parameter by using the parameter name in , such as url by

http://localhost:33064/WebForm2.aspx?reurl=WebForm1.aspx

We want to get reurl Value , It can be written like this :

var xx = getUrlParam('reurl');

got it javascript obtain url Parameter method , We can do this for jquery Extend a method to jquery obtain url Parameters , The following code is jquery Expanded one getUrlParam() Method

 (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);

by jquery After extending this method, we can get the value of a parameter through the following methods :

 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 2 :
(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); // Method 2 :
var xx = $.getUrlParam('reurl'); // Method 1 :
// var xx = getUrlParam('reurl'); alert(xx); }); // Method 1 :
// obtain url Parameters in
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // Construct a regular expression object with target parameters
var r = window.location.search.substr(1).match(reg); // Match target parameters
if (r != null) return unescape(r[2]); return null; // Return parameter value
} </script>

2014-4-23 modify

Today we are using the above method to get url Parameters in ,url No matter how to test the Chinese parameters passed in , It's all garbled . After some debugging, I found that , When I pass the parameter again , The encoding of Chinese characters is  encodeURI , The above method is used to parse the parameter code unescape , It is amended as follows  decodeURI That's all right. .

attach : W3School In the introduction :

JavaScript unescape() function

Definition and Usage

unescape() The function can be used to escape() Decode the encoded string .

Parameters describe
string It's necessary . The string to decode or escape .

explain

This function works like this : By finding the form of %xx and %uxxxx The character sequence of (x Represents a number in hexadecimal ), use Unicode character \u00xx and \uxxxx Replacing such a sequence of characters for decoding .

Tips and notes

notes :ECMAScript v3 Has been removed from the standard unescape() function , And against using it , So we should use decodeURI() and decodeURIComponent() Instead, .

Sum up : javascript The encoding and decoding methods of parameters should be consistent :

escape()   unescape()

encodeURI()   decodeURI()

encodeURIComponent()    decodeURIComponent()

Another one I found online javascript obtain url The method of parameter in :

<script language="JavaScript" type="text/javascript">
function GetUrlParms()
{
var args=new Object();
var query=location.search.substring(1);// Get the query string
var pairs=query.split("&");// Break at comma
for(var i=0;i<pairs.length;i++)
{
var pos=pairs[i].indexOf('=');// lookup name=value
if(pos==-1) continue;// If not, skip
var argname=pairs[i].substring(0,pos);// 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 parameters key:
if(args["id"]!=undefined)
{
// If you want to find parameters key:
var value1 = args["id"] ;
alert(value1);
}</script>

Reprint please indicate the source .

[ Development Notes ]- Use jquery obtain url And url Parameters of the method of more related articles

  1. js/jquery Get the current page URL Address and judge URL Whether the string contains a specific value

    js/jquery Get the current page URL Address and judge URL Whether a string contains a specific value is introduced in this paper jquery/js Get the current page url Address method , stay jquery And js Get the current page from url The method is the same , because jque ...

  2. Java Development Notes ( 107 )URL The format of the address

    URL The full name is Uniform Resource Locator, It means uniform resource locator , Commonly known as network address or web address . Every file and interface on the network , There are corresponding URL website , It specifies how other devices find themselves through a series of paths , Still ...

  3. JQUERY Get the URL Information

    When I used to do a website , We often encounter the category highlight of the current page , In order to let users know which page they are currently on . It's always been in every not The same page writing method . A lot of work , It's not easy to modify . I've been thinking about some easy way to achieve . Later, it was found on the Internet that it can be used to get the current ...

  4. Reprint : js jquery Get the url, obtain frameset Of the page specified in url( Modified )

    Reprint website :http://blog.csdn.net/bestlxm/article/details/6800077 js jquery How to get the current page url, obtain frameset Of the page specified in ur ...

  5. front end jquery Get the URL Information

    When I used to do a website , We often encounter the category highlight of the current page , In order to let users know which page they are currently on . It's always been written in different ways on every page . A lot of work , It's not easy to modify . I've been thinking about some easy way to achieve . Later, it was found on the Internet that it can be used to get the current U ...

  6. [ forward ]jquery Get the URL Information

    When I used to do a website , We often encounter the category highlight of the current page , In order to let users know which page they are currently on . It's always been written in different ways on every page . A lot of work , It's not easy to modify . I've been thinking about some easy way to achieve . Later, it was found on the Internet that it can be used to get the current U ...

  7. 【 Netting 】JS or jQuery Get the URL Information

    1. Set or get the file name or path specified by the object . window.location.pathname 2. Set or get the whole URL For the string . window.location.href 3. Set or get the connection with URL Turn off ...

  8. javascript obtain url The method of corresponding parameter in

    Using regular expressions and location.search Method , The corresponding parameters can be easily obtained :   function getQueryString(name) {var reg = new RegExp(" ...

  9. Java Development Notes ( eighty ) Using reflection techniques to manipulate private methods

    This article describes how to use reflection technology to read and write private properties , It's not just private properties , Even private methods can be called through reflection technology . To demonstrate the anti sky function of reflection , First of all Chicken Chicken added the following private methods , For the sake of simplicity set***/get** ...

Random recommendation

  1. Mobile H5 Page HD multi screen adaptation scheme

    background Develop mobile H5 page Facing different resolutions of mobile phones Facing different screen sizes of mobile phones Visual draft Before front-end development , Vision MM Will give us one psd file , Call it visual draft . For mobile development , In order to achieve the effect of high-definition page , The standard of visual manuscript ...

  2. Java Transaction processing full resolution ( 5、 ... and )—— Template Pattern

    In the last article in this series , We talked about using TransactionManger and ConnectionHolder Complete thread safe transaction management , In this paper , We'll build on that by introducing Template Mode for transaction management . Te ...

  3. springmvc in @PathVariable and @RequestParam The difference between ( Baidu collection )

    http://localhost:8080/Springmvc/user/page.do?pageSize=3&pageNow=2 You can understand the address separately , The first half of the question mark :http://lo ...

  4. J-link V8 Firmware upgrade

    http://blog.sina.com.cn/s/blog_5bdee3020101khfy.html I haven't done my part for electronic engineering for a long time ! I'll try my best today ! Write about how I upgraded J-link The firmware of the ! J- ...

  5. Android System source code download

    lately , I'm studying android, So I want to download android Look at the source code . according to http://source.android.com/source/downloading.html The steps prompted on this page are downloading the source generation ...

  6. Use Microsoft Remote Desktop Mobile remote control windows

    Right click on my computer to select “ attribute ”, Open the property panel . Then click... On the left “ Remote settings ”. 2/2 If you want to operate the computer access to the Internet ( Most of them are connected directly to the computer through the home network cable ), Just select... In the remote desktop selection box “ Allow any version of remote desktop computing to run ...

  7. FFmpeg Simple analysis of source code :avcodec_encode_video()

    ===================================================== FFmpeg Library function source code analysis article list : [ Architecture diagram ] FFmpeg Source code structure - decode F ...

  8. Mysql Hierarchy 、 Execution order 、 Perform plan analysis

    Logical layering Here is MySQL The logical hierarchy diagram of : adjoining course : Connection and thread processing , This floor is not MySQL alone possess , Generally based on C/S Architecture has similar components , For example, connection processing . Authorized certification . Safety, etc . Service layer : Including cache queries . Parser . Optimizer ...

  9. rpm and yum Software management

    rpm brief introduction Database management tools , By reading the database , Determine whether the software is installed , If it has been installed, the location of all the files can be read out , And you can delete these files . rpm:RPM is Redhat Package Manager( recursive ...

  10. Android Studio Code line count plug-in Statistics

    Android Studio It does not provide the function of counting all the lines of the code , But for developers , This function is really necessary ,Statistic It's very convenient to count the number of lines of code , Also very detailed . 1, The first thing is to download the plug-in , Download address : ...