Requesthelper used in Javascript

Source: Internet
Author: User
A small requirement is to display different content based on the input anchor (that is, the content after # in the URL). Remember what I wrote before, I don't know where I threw it, but I have to write another one. By the way, I have improved my functions and added querystring and cookie retrieval. Code .
Requesthelper. js
// Function: Provide querystring/Cookie/anchor access in JavaScript.
/* Usage:
VaR request = new requesthelper ();
VaR S = request. querystring ["ID"]; // obtain the ID parameter in the URL.
VaR c = request. Cookies ["name"]; // obtain the cookie value with the ID as name.
VaR A = request. Anchor; // obtain the name of the anchor located in the URL.
*/
// Updated: 2008-05-31
Requesthelper. Prototype. getparams = function ()
{
VaR result = {};
VaR loc = Document. Location. tostring ();
If (loc. indexof ("? ")>-1)
{
VaR L = LOC. lastindexof ("#")>-1? Loc. lastindexof ("#"): Loc. length;
VaR param_str = LOC. substring (loc. indexof ("? ") + 1, L );
VaR Params = param_str.split ("&");
For (VAR x = 0; x <Params. length; X ++)
{
Params [x] = Params [X]. Split ("= ");
Result [Params [x] [0] = Params [x] [1];
}
}
Return result;
}

Requesthelper. Prototype. getcookies = function ()
{
VaR result = {};
VaR cookie = Document. Cookie;
If (cookie. length> 0)
{
VaR Reg =/(^ [a-zA-z0-9] +? | [A-zA-z0-9] + ?) =/G;
VaR c = cookie. Match (REG );
If (c)
{
VaR n = 0;
For (VAR x = 0; x <C. length; X ++)
{
N = (x <C. Length-1 )? Cookie. indexof (C [x + 1]. tostring (): Cookie. length;
VaR S = cookie. substring (cookie. indexof (C [X]. tostring (), N );
S = S. Split ("= ");
S [0] = s [0]. Replace (/^ ;/,"");
Result [s [0] = s [1];
}
}
}
Return result;
}

Requesthelper. Prototype. getanchor = function ()
{
VaR anchor;
VaR loc = Document. Location. tostring ()
If (loc. lastindexof ("#")>-1)
{
Anchor = LOC. substring (loc. lastindexof ("#") + 1 );
}
Return anchor;
}

Function requesthelper ()
{
This. querystring = This. getparams ();
This. Cookies = This. getcookies ();
This. Anchor = This. getanchor ();
}

After preliminary tests, no problems were found, but there is no guarantee that there are no bugs. If you can use them, copy and paste them as needed. There are better methods. Please give me some advice. I am just a side dish, everyone will be merciful.
In addition, because the cookie name does not support some special characters, only numbers and letters are used here. Please note that if you have other methods, please give us some advice. Haha!

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.