Four Methods for passing parameters between HTML pages using JavaScript

Source: Internet
Author: User

We know that on the server side ASP, JSP, etc.ProgramParameters sent from form on the HTML page can be accepted. So, can we pass parameters to the HTML page. Yes.
Principle: parameters are obtained using the delimiter in window. Location. href.
Method 1:


/*
* Function: obtain parameters from href.
* Shref: http://www.cscenter.com.cn/arg.htm? Arg1 = D & arg2 = Re
* Sargname: arg1, arg2
* Return: the value of Arg. D, re
  */
Function Getargsfromhref (shref, sargname)
{
VaR ARGs = Shref. Split ( " ? " );
VaR Retval =   "" ;

If (ARGs [ 0 ] = Shref) /* The parameter is null. */
{
Return Retval; /* No action required */
}
VaR Str = ARGs [ 1 ];
ARGs = Str. Split ( " & " );
For ( VaR I =   0 ; I < Args. length; I ++ )
{
Str = ARGs [I];
VaR ARG = Str. Split ( " = " );
If (Arg. Length <=   1 ) Continue ;
If (ARG [ 0 ] = Sargname) retval = Arg [ 1 ];
}
Return Retval;
}

Method 2: Function Getvalue (name)
{
VaR Str = Window. Location. search;
If (Str. indexof (name) ! =- 1 )
{
VaR Pos_start = Str. indexof (name) + Name. Length + 1 ;
VaR Pos_end = Str. indexof ( " & " , Pos_start );
If (Pos_end =- 1 )
{
Return Str. substring (pos_start );
}
Else
{
Return Str. substring (pos_start, pos_end)
}
}
Else
{
Return   " No such name value " ;
}
}
Alert (getvalue (name ));

Method 3:

Request = {
Querystring: Function (Item ){
VaR Svalue = Location. Search. Match ( New Regexp ( " [\? \ &] "   + Item +   " = ([^ \ &] *) (\ &?) " , " I " ));
Return Svalue ? Svalue [ 1 ]: Svalue;
}
}
Alert (request. querystring ( " ID " ));

Method 4: VaR URL = Location. search;
VaR Request =   New Object ();
If (URL. indexof ( " ? " ) ! =- 1 )
{
VaR Str = URL. substr ( 1 ); // Remove? No.
STRs = Str. tolowercase ();
STRs = STRs. Split ( " & " );
For ( VaR I = 0 ; I < STRs. length; I ++ )
{
Request [STRs [I]. Split ( " = " )[ 0 ] = Unescape (STRs [I]. Split ( " = " )[ 1 ]);
}
}

VaRMapwidth=Request ["W"];
VaRMapheight=Request ["H"];

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.