Processing cookies using object-oriented ideas

Source: Internet
Author: User

Example: Use the object-oriented method to process cookies
If you are not familiar with cookies, you can learn how to use them in chapter 7, although several
Common functions are used to process cookies. However, these functions are separated from each other, but they do not represent a whole. Lenovo
The math object function in Javascript is actually a Global Object of math, which converts all mathematical calculations
The constants and methods are associated with each other to improve encapsulation and efficiency. Cookie
In fact, this method can also be used.
6.9.1 Requirement Analysis
In fact, Cookie processing only encapsulates some methods, and each object is not in the state, so you do not need to create
Create a cookie processing class and use only one global object to contact these cookie operations. Object name can be understood as life
Namespace. Which of the following operations are considered for Cookie operations:
(1) Setting cookies includes adding and modifying functions. In fact, if the original cookie name already exists
Adding this cookie is equivalent to modifying this cookie. When setting cookies, you may have some other options.
Specifies the cookie declaration cycle, access path, and access domain. This method is used to enable the cookie to store Chinese characters.
You also need to encode the stored values.
(2) Delete a cookie. to delete a cookie, you only need to set the expiration event of a cookie to the previous one.
To delete a cookie.
(3) obtain the value of a cookie. The method receives the cookie name as a parameter and returns the value of the cookie. Because in
This value has been encoded, so the value should be automatically decoded and then returned.
To meet these requirements, the next section will implement these functions.
6.9.2 create a cookie object
Because it is used as a class name or namespace, it is similar to the math object. Here we use cookies to indicate
This object:
VaR cookie = new object ();
6.9.3 how to set cookies
The method is prototype: setcookie (name, value, option); name indicates the name of the cookie to be set; value indicates
Set the cookie value. option includes other options, which are an object as a parameter. The implementation is as follows:
Cookie. setcookie = function (name, value, option ){
// Store the cookie Format String assigned to document. Cookie
VaR STR = Name + "=" + escape (value );
If (option ){
// If the expiration time is set
If (option. expiredays ){
VaR date = new date ();
VaR MS = option. expiredays x 24*3600*1000;
Date. settime (date. gettime () + MS );
STR + = "; expires =" + date. togmtstring ();
}
If (option. Path) STR + = "; Path =" + path; // you can specify an access path.
If (option. domain) STR + = "; domain" + domain; // sets the access host
If (option. Secure) STR + = "; true"; // set security
}
Document. Cookie = STR;
}
6.9.4 how to obtain the cookie value
The method prototype is getcookie (name), where name is the name of the specified cookie, and the corresponding response is returned Based on the name.
. The implementation is as follows:
Cookie. getcookie = function (name ){
VaR cookiearray = Document. Cookie. Split (";"); // obtain the split cookie name value pair.
VaR cookie = new object ();
For (VAR I = 0; I <cookiearray. length; I ++ ){
VaR arr = cookiearray [I]. Split ("="); // separate names and values
If (ARR [0] = Name) return Unescape (ARR [1]); // if it is a specified cookie, its value is returned.
}
Return "";
}
6.9.5 how to delete a cookie
The method is prototype: deletecookie (name). The name indicates the name of the specified cookie.
Delete the corresponding cookie. In this implementation, deleting a cookie is done by calling setcookie.
Specify the expiredays attribute as a negative number:
Cookie. deletecookie = function (name ){
This. setcookie (name, "", {expiredays:-1}); // set the expiration time to the past to delete a cookie.
}
Through the above Code, the entire cookie object is created and can also be defined in a braces,
For example:
VaR cookie = {
Setcookie: function (){},
Getcookie: function (){},
Deletecookie: function (){}
}
In this form, the functions of cookies can be clearer, and as a global object
Cookie operations, such:
Cookie. setcookie ("user", "Jack ");
Alert (cookie. getcookie ("user "));
Cookie. deletecookie ("user ");
Alert (cookie. getcookie ("user "));
The above code first creates a cookie named user, and then deletes the cookie. Two alert outputs
The statement shows the execution result.
This section creates a cookie object to process cookies, which facilitates operations and reflects object-oriented programming.
Idea: encapsulate related functions in an object. Taking into account the characteristics of the Javascript language, this chapter does not require selection
The example of creating class object-oriented programming is not very different from the general object-oriented language. In JavaScript
You can directly create objects to introduce the implementation and working principle of cookie objects. In fact, this is similar to JavaScript.
The working principle of the internal object math is similar.

 

VaR cookie = new object (); cookie = {setcookie: function (name, value, option) {// used to store values assigned to document. cookie Format String var STR = Name + "=" + escape (value); If (option) {// If the expiration time is set if (option. expiredays) {var date = new date (); varMS = option. expiredays * 24*3600*1000; date. settime (date. gettime () + MS); STR + = "; expires =" + date. togmtstring ();} If (option. path) STR + = "; Path =" + path; // set the access path if (option. domain) STR + = "; domain" + domain; // you can specify if (option. secure) STR + = "; true"; // set security} document. cookie = STR ;}, getcookie: function (name) {var cookiearray = document. cookie. split (";"); // obtain the split cookie name value for VAR cookie = new object (); For (VAR I = 0; I <cookiearray. length; I ++) {var arr = cookiearray [I]. split ("="); // separate names and values if (ARR [0] = Name) {// If the cookie is specified, return its value return Unescape (ARR [1]) ;}} return "" ;}, deletecookie: function (name) {This. setcookie (name, "", {expiredays:-1}); // set the expiration time to the past to delete a cookie }}

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.