Handling cookies with OO ideas

zhaozj2021-02-16  44

Creating an OOD of OOD with JavaScipt is rich in elasticity and can be used in a variety of applications. For example, shopping cards, logins, floating windows, etc. You can give full play to your imagination. Download the source program at the end of the text.

Introduction The browser is able to keep relevant data over cookies. Like Windows registry, users don't have to know the specific location of cookies, the browser can find these data. The sixth generation browser, no matter whether IE or NS supports the Document.cookie attribute. Read or modify the value of cookies by this property. However, the storage form of the cookies is a non-structured long string, and it needs to be meaningful after the corresponding analysis.

The expression of cookies is as follows, other than Name = Value, the other is optional: name = value; expires = date; domain = domainnamepath = pathname; Secure;

For example: user = hockeydude; expires = thu, 01-jan-70 00:00:01 gmt; domain = www.mydomain.com; Path = / images; second; pass = gretzky; Expires = THU, 01-JAN-70 00:00:01 gmt; domain = www.mydomain.com; path = / image.;

Such a long two string only represent two cookies. If you need to add an electronic mailbox or other information, you have to extend a string. Usually, each variable or element is obtained by decomposing such a string. This is really cost-effective.

I use the object-oriented design (Object Oriented Design, OOD) to write the cookies handler, which features the following characteristics:

Easy to increase the deeper. This is very important, some browser limits the number of cookies. Time data can be easily modified by modifying a function. The usual practice is very troublesome, copy cookies, delete the original cookies, modify and override the cookies. Cookies and its submers are stored in an array. Modifications can be made quickly and efficiently as needed. This also does not need to parse the long string.

The following is the public method of the object:

Square bracket [] is an optional parameter // constructs cookies ([Delivery, Default State]) - Constructor

// Initialize getCookieCount () - Return to cookies Quantity Fetch (INDEX) - Returns the cookies name of the specified index Create (Name, Days) - Creating a cookies and its time-operated number modify (name, days) - Modify the number of days of cookies DELETE (Name) - Remove cookies and its child getCount (Name) - Return to Cookies Number AddItem (Name, Value) - Add a child getItem (Name, INDEX) - Returns the child Delitem (Name, Index) - Delete Specified child

// Access Load () - Read CookiesSave () - Storage Cookies

Below is an application example: