JavaScript gets parameters from URL

Source: Internet
Author: User

How to get the parameters from the URL is a very basic problem. First assume that the URL is legal, code as follows, welcome you big code Review.

1. You can use a match and loop split

function findqueriesfromurl (URL) {    var regex, matches, I, length, pair, result = {}, query;     if return ;     =/\w+\=\w*/g;     = Url.match (regex);      for (i = 0, length = matches.length;i<length;i++) {        = matches[i].split (' = ');        Result[decodeuricomponent (pair[0])] = decodeURIComponent (pair[1]);     }         return result;}

2. You can use EXEC to get all the groupings,

function findqueriesfromurl (URL) {    var regex, result = {}, match;     if return ;     =/(\w+) \= (\w*)/g;//No/g will die loop.      while (match = regex.exec (URL)) {
result[decodeuricomponent (match[1])] = decodeURIComponent (match[2]); } return result;}

It is important to note that exec only iterates over the same calling object and the reference of the same string as the argument , otherwise it may die loops, such as the following code:

 while (match = (/(\w+) \= (\w*)/g). EXEC (URL)) {result[match[1]] = match[2];}

aside, the EXEC function loops back all of the match's objects, such as calling

(function  excute (URL) {    var regex, result = {}, match, i = 0;     =/(\w+) \= (\w*)/g;      while (I <) {        i+ +;        Console.log (regex.exec (URL));    }         return result;}) (' a=b ');

Will loop through the output of a match object and NULL, as follows:

["A=b", "a", "B", index:0, Input: "A=b"] NULL ["A=b", "a", "B", index:0, Input: "A=b"] NULL ["A=b", "a", "B", index:0, Input: "A=b"] NULL ["A=b", "a", "B", index:0, Input: "A=b"] NULL ["A=b", "a", "B", index:0, Input: "A=b"] NULL

If you remove the g inside the regular expression, the output will become:

["A=b", "a", "B", index:0, Input: "A=b"] ["A=b", "a", "B", index:0, Input: "A=b"] ["A=b", "a", "B", Inde x:0, Input: "A=b"["A=b", "a", "B", index:0, Input: "A=b"] ["A=b", "a", "B", index:0, Input: "A=b"  ["A=b", "a", "B", index:0, Input: "A=b"] ["A=b", "a", "B", index:0, Input: "A=b"] ["A=b", "a", "B" , index:0, Input: "A=b"["A=b", "a", "B", index:0, Input: "A=b"] [

This will not be able to take all the matches.

JavaScript gets parameters from URL

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.