JavaScript gets an example of getting parameters in a URL

Source: Internet
Author: User

In the front-end development, we often need to pass between the pages of the get parameters to use JS to achieve some effect, this involves a method: How to detach from the current page of the URL of the get parameters we need.

The URL that contains the get parameter is as follows: Index.php?a=11&b=abc&d=51, let me show you what to do if you obtain getting parameters.

1, get to the current page URL in the dynamic string

JS gives us a good way: Location.search, can get to the current page URL '? ' After the string.

The code is as follows Copy Code

var str = Location.search;

2, to get the string for the first time division

The code is as follows Copy Code
var temp = Str.split (id+ "=") [1] | | "";//If a get parameter is cut, no is null

The ID here is the name we need to get the value of the parameter.

3, the second Division

The code is as follows Copy Code
Temp.indexof ("&") >=0? Temp.split ("&") [0]: temp;//If there is a ' & ' continue to split, there is no direct return

The value returned is already the corresponding parameter value of the ID we want, and the complete method is as follows:

The code is as follows Copy Code

function Getlocationval (ID) {
var temp = Location.search.split (id+ "=") [1] | | "";
Return Temp.indexof ("&") >=0? Temp.split ("&") [0]: temp;
}

After we get to the parameter value, we can do the relevant DOM operation.

Let me give you an example

The code is as follows Copy Code

function Getlocationparameter (param) {
var request = {
Querystring:function (val) {
var uri = Window.location.search;
var re =new RegExp ("+ val +" = ([^&?] *) "," IG ");
Return (Uri.match (re))? (decodeURI (Uri.match (re) [0].substr (Val.length + 1)): ");
}
}
Return request. QueryString (param);
}

Attach another

  code is as follows copy code

Code Highlighting produced by Actipro Codehighlighter (freeware) http://www. Codehighlighter.com/-->//lastest:
var querystrings=function () {//get URL querystring
    var params=document.location.search,reg=/(?: ^\?| &) (. *?) =(.*?) (? =&|$)/g,temp,args={};
    while (temp=reg.exec (params))!=null args[temp[1]]=decodeuricomponent (temp[2));
    return args;
};
//Only one:
var querystring=function (key) {
    return Document.location.search.match (new RegExp ("(?: ^\\?| &) "+key+" = (. *?) (? =&|$)) | | [", NULL]) [1];
}
Var args=querystrings ();
Alert (args.name+ | "+args.sex+" | "+args.age);
//Test Link: <a href= "? name=abc&sex= male &age=12" >test getquerystring</a>

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.