How to obtain url parameters in JS

Source: Internet
Author: User

① A student wrote this on the Internet:

Function QueryString (key ){
Var paras = location. search;
If (paras ){
Var arr = paras. substr (1). split ("&"), data;
For (I in arr ){
Data = arr [I]. split ("= ");
If (data [0] = key ){
Return data [1]
}
}
}
} Veryhuo.com

② This was originally collected and modified: liehuo.net

I added a decodeURIComponent method.If you add or not, I add

// If you use a page parameter with Chinese characters to jump to the page,
// The URL is similar to this (http: // xxxx/MyJsp. jsp? Id = 3 & name = haha ):
// Travel 3 converts Chinese characters to http: // xxxx/MyJsp. jsp? Id = 3 & name = % BA % C7 % BA % C7
// In IE, http: // xxxx/MyJsp. jsp by default? Id = 3 & name = haha

Function Request (name ){
Var pattern =/(\ w *) = ([a-zA-Z0-9 \ u4e00-\ u9fa5] +)/ig, params = {}; // define a regular expression and an empty object
DecodeURIComponent (window. location. href, true). replace (pattern, function (a, B, c) {params [B] = c ;});
Return params [name]? Params [name]: null;
}
// Method for Evaluating the. Net Indexer
Function Params (){
Var pattern =/(\ w *) = ([a-zA-Z0-9 \ u4e00-\ u9fa5] +)/ig, params = {}; // define a regular expression and an empty object
DecodeURIComponent (window. location. href, true). replace (pattern, function (a, B, c) {params [B] = c ;});
Return params;
}
Var params = Params ();
Alert (params ["name"]);
// Of course, liehuo.net
// Alert (params. name); fire net

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.