JS to get the parameter values in the URL

Source: Internet
Author: User

/**//*
Get the value of the last parameter in the URL
*/

VaR STR = Window. Location. href;
// Alert (STR );
VaR es =/souceid = /;
Es.exe C (STR );
VaR right = Regexp. rightcontext;
// Alert (right );

// Column
<Script language = "JavaScript">
VaR STR = Window. Location. href;
VaR es =/souceid = /;
Es.exe C (STR );
VaR right = Regexp. rightcontext;
// Alert (right );
Switch (right ){
Case 'din ':
Case 'exh ':
Case 'banks ':
Case 'shop ':
Case 'treat ':
Case 'trip ':
Chgtab ('tab3', 'tabcontent3 ');
Break;
Case 'air ':
Case 'railway ':
Case 'road ':
Case 'subway ':
Chgtab ('tab2', 'tabcontent2 ');
Break;
Default:
Chgtab ('tab1', 'tabcontent1 ');
}
</SCRIPT>

// The following describes how to write a function.
Function getparam (){
VaR url = Document. Location. href;
VaR name = ""
If (URL. indexof ("=")> 0)
{
Name = URL. substring (URL. indexof ("=") + 1, URL. length)
}
Return name;
}

/**//*
Obtains the specified URL parameter value.
URL: http://www.blogjava.net/blog? Name = BAINIAN
Parameter: paramname URL parameter
Call method: getparam ("name ")
Return Value: BAINIAN
*/
// 1.
Function getparam (paramname)
{
Paramvalue = "";
Isfound = false;
If (this. Location. Search. indexof ("? ") = 0 & this. Location. Search. indexof (" = ")> 1)
{
Arrsource = Unescape (this. Location. Search). substring (1, this. Location. Search. Length). Split ("&");
I = 0;
While (I <arrsource. Length &&! Isfound)
{
If (arrsource [I]. indexof ("=")> 0)
{
If (arrsource [I]. Split ("=") [0]. tolowercase () = paramname. tolowercase ())
{
Paramvalue = arrsource [I]. Split ("=") [1];
Isfound = true;
}
}
I ++;
}
}
Return paramvalue;
}

// 2.
Function request (sname)
{

/**//*
Get last LOC.?
Right: Find first LOC. Of sname
+ 2
Retrieve value before next &

*/

VaR Surl = new string (window. Location );
VaR Surl = Document. Location. href;
VaR iqmark = Surl. lastindexof ('? ');
VaR ilensname = sname. length;

// Retrieve LOC. Of sname
VaR istart = Surl. indexof ('? '+ Sname +' = ') // limitation 1
If (istart =-1)
{// Not found at start
Istart = Surl. indexof ('&' + sname + '=') // limitation 1
If (istart =-1)
{// Not found at end
Return 0; // not found
}
}

Istart = istart ++ ilensname + 2;
VaR itemp = Surl. indexof ('&', istart); // next pair start
If (itemp =-1)
{// EOF
Itemp = Surl. length;
}
Return Surl. Slice (istart, itemp );
Surl = NULL; // destroy string
}

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.