Parse URL strings in HTML pages using JavaScript

Source: Internet
Author: User
/// Reads the URL string as a key-value pair.

///For exampleHttp: // 192.168.1.211/jcysoft6.0 _ Changzhi/vacation.htm? Id = 1 & Action = add
FunctionGetquerystring (key)

{

VaRValue = "";

///Get the URL of the current page

VaRSurl = Invalid Response Doc ument. url;

/// URLWhether the query string is included in

If(Surl. indexof ("? ")> 0)

{

//Break down the URL. The second element is the complete query string.

//That is, the value of arrayparams [1] is [ID = 1 & Action = 2]

VaRArrayparams = Surl. Split ("? ");

//Break down query strings

// Arrayurlparams [0]The value is [ID = 1]

// Arrayurlparams [2]The value is [Action = add]

VaRArrayurlparams = arrayparams [1]. Split ("&");

//Traverse the decomposed key-value pairs

For(VaRI = 0; I <arrayurlparams. length; I ++)

{

//Break down a key-Value Pair

VaRSparam = arrayurlparams [I]. Split ("= ");

If(Sparam [0] = Key) & (sparam [1]! = ""))

{

//The matched key is found and the value is not empty.

Value = sparam [1];

Break;

}

}

}

ReturnValue;

}

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.