A simple hashtable written by JS is mainly used to add URL parameters to the client, such as param1 = value1 & param2 = value2 & param3 = value3.
There are some JS frameworks with similar methods, such as the famous prototype framework, and it also provides all form elements, directly serialize into URL parameters. Used a long time ago
For a while, but jquery is usually used now. Let's talk nonsense, make contributions, and discuss and criticize with everyone.
/*
* Create by HJP
* Blog: http://jackhuclan.cnblogs.com
* 2009-01-08
* Usage for append or remove parameters to URL
*/
VaR Hashtable = Function Sg_hashtable ()
{
This . _ Hash = New Activexobject ( " Scripting. Dictionary " );
This . Items = [];
This . _ Keys = Null ;
This . _ Values = Null ;
}
Hashtable. Prototype = {
Add: Function (Key, value)
{
If ( This . _ Hash. exists (key ))
{
This . _ Hash. Remove (key );
VaR Pos = This . Find (key );
If (Pos ! =- 1 )
{
This . Items [POS]. Value = Value;
}
This. _ Hash. Add (Key, value );
}
Else
{
This . _ Hash. Add (Key, value );
This . Items. Push ({ " Key " : Key, " Value " : Value });
}
},
Remove: Function (Key)
{
If ( This . _ Hash. exists (key ))
{
This . _ Hash. Remove (key );
VaR Pos = This . Find (key );
If (Pos ! =- 1 )
{
VaR Newarr = This . Items. Slice ( 0 , POS). Concat ( This . Items. Slice (Pos + 1 , This . Items. Length ));
Delete This . Items;
This . Items = Newarr;
}
}
},
Removeall: Function ()
{
This . _ Hash. removeall ();
Delete This . Items;
This . Items = [];
},
Getkeys: Function (){
This . _ Keys = [];
For ( VaR I = 0 ; I < This . Items. length; I ++ )
{
VaR Item = This . Items [I];
This . _ Keys. Push (item. Key );
}
Return This . _ Keys;
},
Getvalues: Function (){
This . _ Values = [];
For ( VaR I = 0 ; I < This . Items. length; I ++ )
{
VaR Item = This . Items [I];
This . _ Values. Push (item. value );
}
Return This . _ Values;
},
Tourl: Function ()
{
VaR Str = "" ;
For ( VaR I = 0 ; I < This . Items. Length - 1 ; I ++ )
{
VaR Item = This . Items [I];
If (Encodeuricomponent)
Str + = Encodeuricomponent (item. Key) + " = " + Encodeuricomponent (item. value) + " & " ;
Else
Str + = Escape (item. Key) + " = " + Escape (item. value) + " & " ;
}
VaR Lastitem = This . Items [ This . Items. Length - 1 ];
If (Encodeuricomponent)
Str + = Encodeuricomponent (lastitem. Key) + " = " + Encodeuricomponent (lastitem. value );
Else
Str + = Escape (lastitem. Key) + " = " + Escape (lastitem. value );
Return STR;
},
Find: Function (Key)
{
For ( VaR I = 0 ; I < This . Items. length; I ++ )
{
VaR Item = This . Items [I];
If (Item. Key = Key)
{
Return I;
}
}
Return - 1 ;
}
}
The client call is as follows:
< Script SRC = " Sg_hashtable.js " Type = " Text/JavaScript " > < / SCRIPT>
< Script Type = " Text/JavaScript " >
VaR HS = New Hashtable ();
HS. Add ( " Id1 " , " 1 " );
HS. Add ( " Id2 " , " Zhang San " );
HS. Add ( " Id2 " , " Dddd " );
HS. Add ( " ID4 " , " 4 " );
VaR Str = HS. tourl ();
Debugger
HS. getkeys ();
HS. getvalues ();
HS. Remove ( " ID4 " );
Debugger
HS. removeall ();
< / SCRIPT>