Jump to content

Writing Json Cookies


gw1500se

Recommended Posts

I don't know if I have a mental block or if the documentation is just poor. In any case I am trying to create a cookie with this code:

const daysToExpire = new Date(2147483647 * 1000).toUTCString();
document.cookie=escape("Auto_Select_0519669="+JSON.stringify(json_array)+";expires="+daysToExpire+";");

Nothing is written. What am I dong wrong or not doing? TIA.

Link to comment
Share on other sites

Using that library, at least the way I read it, does not work. This is my function which produces "undefined" in the console log (no errors):

function create_cookie(obj=null) {
    var json_array=[];
    if (obj==null) {
        json_array=[{filter:false}];
    }
    else {
        json_array=obj;
    }
    Cookies.set("Auto_Select_0519669",JSON.stringify(json_array),{expires:2147483647});
}
console.log(Cookies.get("Auto_Select_0519669"));

 

Link to comment
Share on other sites

Now that I have another issue fixed, I am back to the problem of no cookie being written. This is my current code:

function create_cookie(obj=null) {
    var json_array=[];
    if (obj==null) {
        json_array=[{none:true}];
    }
    else {
        json_array=obj;
    }
    Cookies.set("Auto_Select_0519669",JSON.stringify(json_array),{expires:3650});
}

var json_str=Cookies.get("Auto_Select_051969");
if (typeof(json_str)=="undefined") {
   create_cookie();
   json_str=[{none:true}];
   console.log("set new cookie");
}
console.log(json_str);

Each time I run this code I get the output "set new cookie". When I search for the cookie it it not found. Apparently 'Cookies.set' is not writing the cookie but I get no errors.

Edited by gw1500se
Link to comment
Share on other sites

I feel like something else is happening - this code works for me:

	<script src="https://cdn.jsdelivr.net/npm/js-cookie@3.0.1/dist/js.cookie.min.js"></script>
	<script>
		function setCookie(){
			Cookies.set('testing2', JSON.stringify([{
				testing1: 'hi there',
				testing2: 'whaddup?!?',
				testing3: 'how you doing?',
			}]), { expires: 3650 });
		}

		var json_string = Cookies.get('testing2');

		console.log(json_string);

		if(typeof json_string == 'undefined'){
			setCookie();
			json_string = Cookies.get('testing2');
		}

		console.log(json_string);
	</script>

First time I visit the page, i get 'undefined' followed by the JSON string. Subsequent visits give me the JSON string both times.

The only real difference here is that I'm setting json_string from the cookie after I set it where you're hard-coding the value. But even then, you shouldn't be seeing the 'set new cookie' console log because after the first page load the cookie would be set.

What does your entire script look like? Are you getting any errors?

Link to comment
Share on other sites

For a chrome extension I'm pretty sure you have to assign permissions in your manifest file, if you want a specific host to have access.  Then use chrome.cookies api.  See:  https://developer.chrome.com/docs/extensions/reference/cookies/

I'm not sure what you are trying to do with cookies in your extension, but an alternative that isn't chrome specific would be to use local storage.  Again you need to specify in the manifest:  https://developer.chrome.com/docs/extensions/reference/storage/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.