COOKIE
- Each will carry a HTTP request cookie header in the request to the server
- Each will carry the cookie HTTP response to the client in response header
- In other words, cookie that we do not need to manually set up, it will automatically walk between the client and server data
-
-
Each
key=value
is a piece of data -
Among the plurality of data
;
// form the cookie 'a = 100; b = 200 ; c = 300;'
-
-
Limited quantity, generally about 50
-
Timeliness, is there an expiration time, usually a session level (that is, the browser closes its expiration)
-
cookie = const document.cookie console.log (cookie) // will be able to get the current value of the cookie
// set the timeliness of a session-level cookie the document.cookie = 'A = 100' // provided a cookie expiration time document.cookie = 'b = 200; expires = Thu, 18 Dec 2043 12:00:00 GMT "; ' // above the cookie data will later at 12:18 on December 2043 Ri expired, expired will automatically disappear after
// because the cookie can not directly delete // before so we can put an expiration date of a cookie set to the current time // the browser will automatically delete the cookie document.cookie = 'b = 200; the Expires = Thu, 18 Dec 2018 12:00:00 GMT "; '
<Script> // judged whether or not the object function isObject (Data) { // determined data types must object, date can not be empty (null === because typeof "Object") // data.constructor distinguish arrays and objects return ( typeof Data === "Object" Data &&! == null && && data.constructor data.constructor === Object) } // object consolidation function ASSIGN () { var target = arguments [0 ] for ( var I =. 1 ; i <arguments [i];i++){ for(var attr in arguments[i]){ target [attr] = arguments [I] [attr] } } return target } // delete the Cookie function removeCookie (name, Options) { Cookie (name, "", isObject (Options) ASSIGN (Options, {Expires:? -1 }): {path: Options, Expires: -1 }) } // set the cookie and read cookie function the setCookie (name, value, Options) { // make option does not pass when the parameter is a target IF (the arguments.length >. 1 && typeof value === "String" ) { IF (! isObject (Options)) { options = {}; } if(typeof options.expires === "number"){ var d = new Date(); d.setDate(d.getDate() + options.expires) } return ( document.cookie = [ name + "=" + value, typeof options.domain === "string" ? ";domain=" + options.domain : "", typeof options.path === "string" ? ";path=" + options.path : "" }"test","hello",{ expires : 5 }) var res = cookie("test"); console.log(res); </script>