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!