Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8177167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:26:35+00:00 2026-06-06T23:26:35+00:00

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

  • 0

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 read many posts, but do not know how to serialize appropriately.


EDIT:
Code:

var instances = {};
...
instances[strInstanceId] = { container: oContainer };
...
instances[strInstanceId].plugin = oPlugin;
...
JSON.stringify(instances); 
// throws error 'TypeError: Converting circular structure to JSON'
  1. How do I serialize instances?

  2. How do I maintain functionality, but change structure of instance to be able to serialize with stringify?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:26:36+00:00Added an answer on June 6, 2026 at 11:26 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to store the positions of some elements on the front-end(jQuery) in the
I want jQuery or PHP code tell the visitor if the Javascript is disabled.
Short version: I'm extending a jQuery object function that is intended to take 0-3
I want to associate a JavaScript object with an HTML element. Is there a
I'm using javascript and jQuery. I have a problem that my web app has
I have a website that consists of one parent page. Using the lightbox-style jQuery
My question is quite simple: I want to store the FILE NAME that I
When I store a jQuery object in a variable, like this: var $myObject =
I'm writing a jQuery plugin that works on a piece of JSON data object.
I am using the .data() function in jQuery to store an array as per

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.