A simple hashtable written by JS

Source: Internet
Author: User

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>

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.