Pure Javascript - store object in cookie

前端 未结 4 969
无人及你
无人及你 2020-11-28 05:45

No jQuery.

I want to store an object or array in a cookie.

The object should be usable after page refresh.

How do I do that with pure JavaScript? I

4条回答
  •  Happy的楠姐
    2020-11-28 06:20

    Try that one to write

    function bake_cookie(name, value) {
      var cookie = [name, '=', JSON.stringify(value), '; domain=.', window.location.host.toString(), '; path=/;'].join('');
      document.cookie = cookie;
    }
    

    To read it take:

    function read_cookie(name) {
     var result = document.cookie.match(new RegExp(name + '=([^;]+)'));
     result && (result = JSON.parse(result[1]));
     return result;
    }
    

    To delete it take:

    function delete_cookie(name) {
      document.cookie = [name, '=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.', window.location.host.toString()].join('');
    }
    

    To serialize complex objects / instances, why not write a data dump function in your instance:

    function userConstructor(name, street, city) {
       // ... your code
       this.dumpData = function() {
         return {
            'userConstructorUser': {
                name: this.name,
                street: this.street,
                city: this.city
             }
           }
        }
    

    Then you dump the data, stringify it, write it to the cookie, and next time you want to use it just go:

      var mydata = JSON.parse(read_cookie('myinstances'));
      new userConstructor(mydata.name, mydata.street, mydata.city);
    

提交回复
热议问题