Javascript retrieves an instance of url parameters

Source: Internet
Author: User

Today, a friend asked the webmaster about how to obtain the url parameter value. In webpage design, sometimes we need to obtain the Link parameter on the client, A common method is to use a link as a string, break it down according to the link format, and then obtain the corresponding parameter value. The fire shows the specific implementation of this process.

Of course, we can also use regular expressions for direct matching. This article also provides an example of regular expressions.

Link decomposition method:

<Script type = "text/javascript">
<! --
// Description: Javascript method for obtaining url parameters
// Finishing: http://www.bkjia.com

Function getQueryString (name)
{
// If the link does not have a parameter or the link does not contain the parameter we want to obtain, a blank result is returned.
If (location. href. indexOf ("? ") =-1 | location. href. indexOf (name + '=') =-1)
{
Return '';
}

// Obtain the parameters in the Link
Var queryString = location. href. substring (location. href. indexOf ("? ") + 1 );

// What are the separation parameter pairs? Key = value & key2 = value2
Var parameters = queryString. split ("&");

Var pos, paraName, paraValue;
For (var I = 0; I <parameters. length; I ++)
{
// Obtain the equal sign position
Pos = parameters [I]. indexOf ('= ');
If (pos =-1) {continue ;}

// Obtain the name and value
ParaName = parameters [I]. substring (0, pos );
ParaValue = parameters [I]. substring (pos + 1 );

// If the query name is equal to the current name, the current value is returned. At the same time, the + sign in the link is restored to a space.
If (paraName = name)
{
Return unescape (paraValue. replace (// ++/g ,""));
}
}
Return '';
};

// Http: // localhost/test.html? Aa = bb & test = cc + dd & ee = ff
Alert (getQueryString ('test '));
// -->
</Script>

Regular Expression matching:

<Script type = "text/javascript">
<! --
Function getQueryStringRegExp (name)
{
Var reg = new RegExp ("(^ | \\? | &) "+ Name +" = ([^ &] *) (\ s | & | $) "," I ");
If (reg. test (location. href) return unescape (RegExp. $2. replace (/\ +/g, ""); return "";
};

// Http: // localhost/test.html? Aa = bb & test = cc + dd & ee = ff
Alert (getQueryStringRegExp ('test '));
// -->
</Script>

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.