gw1500se Posted August 23, 2022 Share Posted August 23, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315224-writing-json-cookies/ Share on other sites More sharing options...
maxxd Posted August 23, 2022 Share Posted August 23, 2022 When dealing with cookies in Javascript, I usually use this library. In my experience it's just easier and less clunky. Quote Link to comment https://forums.phpfreaks.com/topic/315224-writing-json-cookies/#findComment-1599703 Share on other sites More sharing options...
gw1500se Posted August 23, 2022 Author Share Posted August 23, 2022 I have to let my javascript ignorance show here. It seems that package required 'imports'. Since I am writing a Chrome extension, imports are not permitted. I need the js file(s) themselves and I don't see that in the git tree. Quote Link to comment https://forums.phpfreaks.com/topic/315224-writing-json-cookies/#findComment-1599705 Share on other sites More sharing options...
kicken Posted August 23, 2022 Share Posted August 23, 2022 16 minutes ago, gw1500se said: need the js file(s) themselves and I don't see that in the git tree You can get them from the releases area. Quote Link to comment https://forums.phpfreaks.com/topic/315224-writing-json-cookies/#findComment-1599707 Share on other sites More sharing options...
gw1500se Posted August 23, 2022 Author Share Posted August 23, 2022 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")); Quote Link to comment https://forums.phpfreaks.com/topic/315224-writing-json-cookies/#findComment-1599712 Share on other sites More sharing options...
kicken Posted August 23, 2022 Share Posted August 23, 2022 Are you calling your create_cookie function somewhere prior to trying to read the cookie? The code you posted doesn't, so undefined would be expected. Quote Link to comment https://forums.phpfreaks.com/topic/315224-writing-json-cookies/#findComment-1599717 Share on other sites More sharing options...
gw1500se Posted August 24, 2022 Author Share Posted August 24, 2022 (edited) Deleted for now. Edited August 24, 2022 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/315224-writing-json-cookies/#findComment-1599732 Share on other sites More sharing options...
gw1500se Posted August 25, 2022 Author Share Posted August 25, 2022 (edited) 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 August 25, 2022 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/315224-writing-json-cookies/#findComment-1599818 Share on other sites More sharing options...
maxxd Posted August 26, 2022 Share Posted August 26, 2022 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? Quote Link to comment https://forums.phpfreaks.com/topic/315224-writing-json-cookies/#findComment-1599849 Share on other sites More sharing options...
gw1500se Posted August 26, 2022 Author Share Posted August 26, 2022 That is the entire script and no, I am not getting an error. The big difference is that my code is running as a Chrome extension. Quote Link to comment https://forums.phpfreaks.com/topic/315224-writing-json-cookies/#findComment-1599853 Share on other sites More sharing options...
gw1500se Posted August 26, 2022 Author Share Posted August 26, 2022 This appears to be related to special restrictions imposed by Chrome extensions. I've moved to stackoverflow for help. Quote Link to comment https://forums.phpfreaks.com/topic/315224-writing-json-cookies/#findComment-1599862 Share on other sites More sharing options...
maxxd Posted August 26, 2022 Share Posted August 26, 2022 Ah, well that would explain it. I forgot about the Chrome extension bit... Quote Link to comment https://forums.phpfreaks.com/topic/315224-writing-json-cookies/#findComment-1599865 Share on other sites More sharing options...
gizmola Posted August 31, 2022 Share Posted August 31, 2022 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/ Quote Link to comment https://forums.phpfreaks.com/topic/315224-writing-json-cookies/#findComment-1599922 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.