Two ways to get URL parameters for JS

Source: Internet
Author: User

JS get parameters, in the past I was used to split, and then get, this way feeling is the simplest

Mode 1:

function QueryString (item) {    var svalue=location.search.match (new RegExp ("[\?\&]" +item+ "= ([^\&]*) (\&?)", "I"))    return svalue?svalue[1]:svalue}//  Use method //url=http://www.xxx.com?id=100&name=domvar id=querystring ("ID" );   var name=querystring ("name");


In this way, every time you call QueryString () This method will parse the URL once (of course, many parameters of the case will not be slow to parse)

Mode 2:

           functiongetrequest () {varurl = location.search;//gets the URL in the "?" String after the character                  varTherequest =NewObject ();//put parameters into this object                 if(Url.indexof ("?")! =-1) {                   varstr = URL.SUBSTR (1); STRs= Str.split ("&"); //Storage Parameters                     for(vari = 0; i < strs.length; i + +) {Therequest[strs[i].split ("=") [0]]= (Strs[i].split ("=") [1]); }                  }                  returntherequest; }//How to use                varRequest =NewObject (); Request=getrequest (); //url--http:www.xxx.com?id=1&&kid=2                 varid=requesr["id"];

This way the parameters are stored in one object at a time, and the URL is parsed again when the argument is taken.

Two ways to get URL parameters for JS

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.