js in the cookie use

COOKIE

  • cookie It is stored in a location character string data
  • 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
  • We just need to set the contents of the cookie can

COOKIE storage form

  • cookie is stored as a string to the string key=valueof the form

  • Each key=valueis a piece of data

  • Among the plurality of data ;divided

// form the cookie 
'a = 100; b = 200 ; c = 300;'

COOKIE features

  1. Limited storage size, generally about 4 KB

  2. Limited quantity, generally about 50

  3. Timeliness, is there an expiration time, usually a session level (that is, the browser closes its expiration)

  4. There are domain name restrictions, that is to say who can read who set

Use

Read the contents of the cookie use document.cookie

cookie = const document.cookie 
console.log (cookie) // will be able to get the current value of the cookie

Content settings cookie usage document.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


Delete the contents of the cookie use document.cookie

// 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 "; '

Packaging operations COOKIE

<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>

 

 

Guess you like

Origin www.cnblogs.com/mz33/p/12596026.html