Proper decoding using Vanilla Javascript
-
I have this cookie that I wrote in PHP, and I need to read the cookie to get the Project Number. I tried using
decodeUri
but it strips the semi colon out of the JSON. I also plagurized some JavaScript to get the cookie in the first place. If I use this [URL Decoder/Encoder](https://meyerweb.com/eric/tools/dencoder/) it gives me the proper decoding that I need. It looks like URL encoding to me. cookie: {"timeStamp"%3A"02\%2F17\%2F2022 10%3A15%3A02"%2C"coreMode"%3A"open"%2C"}function getCookie(name) {
let cookie = {}; document.cookie.split(';').forEach(function(el) { let \[k,v\] = el.split('='); cookie\[k.trim()\] = v; }) return cookie\[name\];
}
If it ain't broke don't fix it Discover my world at jkirkerx.com
-
I have this cookie that I wrote in PHP, and I need to read the cookie to get the Project Number. I tried using
decodeUri
but it strips the semi colon out of the JSON. I also plagurized some JavaScript to get the cookie in the first place. If I use this [URL Decoder/Encoder](https://meyerweb.com/eric/tools/dencoder/) it gives me the proper decoding that I need. It looks like URL encoding to me. cookie: {"timeStamp"%3A"02\%2F17\%2F2022 10%3A15%3A02"%2C"coreMode"%3A"open"%2C"}function getCookie(name) {
let cookie = {}; document.cookie.split(';').forEach(function(el) { let \[k,v\] = el.split('='); cookie\[k.trim()\] = v; }) return cookie\[name\];
}
If it ain't broke don't fix it Discover my world at jkirkerx.com
How are you generating that cookie value? It looks like URL-encoded JSON, but the JSON is invalid.
"{\"timeStamp\":\"02/17/2022 10:15:02\",\"coreMode\":\"open\",\"}"
That trailing quote before the closing brace doesn't match an opening quote. Attempting to parse the value will give you an error:
const value = JSON.parse(decodeURIComponent('{"timeStamp"%3A"02\%2F17\%2F2022 10%3A15%3A02"%2C"coreMode"%3A"open"%2C"}'));
// JSON.parse: unterminated string at line 1 column 56 of the JSON data
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
How are you generating that cookie value? It looks like URL-encoded JSON, but the JSON is invalid.
"{\"timeStamp\":\"02/17/2022 10:15:02\",\"coreMode\":\"open\",\"}"
That trailing quote before the closing brace doesn't match an opening quote. Attempting to parse the value will give you an error:
const value = JSON.parse(decodeURIComponent('{"timeStamp"%3A"02\%2F17\%2F2022 10%3A15%3A02"%2C"coreMode"%3A"open"%2C"}'));
// JSON.parse: unterminated string at line 1 column 56 of the JSON data
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I generated that cookie with PHP 7.4. For some reason it encodes the JSON, but it reads clean with PHP 7.4 I pulled that example out of storage in the browser, and cut off a large section just to use as an example. This is the cookie straight out of the browser storage. Pretty ugly but that's just how it writes even with pretty print.
%7B%22timeStamp%22%3A%2202%5C%2F18%5C%2F2022%2009%3A41%3A35%22%2C%22coreMode%22%3A%22open%22%2C%22projectIntegrityCheck%22%3Afalse%2C%22projectNumber%22%3A%225342%22%2C%22projectType%22%3A%22poolspa%22%2C%22projectStage%22%3A%22designing%22%2C%22projectVersion%22%3A0%2C%22projectFinalVersion%22%3A0%2C%22projectJob%22%3A0%2C%22projectEmployeeId%22%3A91%2C%22projectEmployeeName%22%3A%22Jim%20Miller%22%2C%22projectCustomerId%22%3A%225324%22%2C%22projectCustomerName%22%3A%22Jim%20Kirker%202022021602%22%2C%22projectAddNumber%22%3A-1%2C%22projectSalesId%22%3A%2291%22%2C%22originalBookPrice%22%3A32761.2153%2C%22bookPrice%22%3A32761.2153%2C%22preBookPrice%22%3A32761.2153%2C%22contractAmount%22%3A0%2C%22inputId%22%3A%22%22%2C%22inputName%22%3A%22Selection%20Name%3A%22%2C%22inputPrice%22%3A0%2C%22inputMode%22%3A%22ADD%22%2C%22addTotal%22%3A0%2C%22bookDifference%22%3A0%2C%22userName%22%3A%22jimk%22%2C%22userType%22%3A%22Executive%22%2C%22versionNumber%22%3A-1%2C%22versionBookPrice%22%3A32761.2153%2C%22salesCommission%22%3A0%7D
The same cookie URL decoded ...{"timeStamp":"02\/18\/2022 09:41:35","coreMode":"open","projectIntegrityCheck":false,"projectNumber":"5342","projectType":"poolspa","projectStage":"designing","projectVersion":0,"projectFinalVersion":0,"projectJob":0,"projectEmployeeId":91,"projectEmployeeName":"Jim Miller","projectCustomerId":"5324","projectCustomerName":"Jim Miller 2022021602","projectAddNumber":-1,"projectSalesId":"91","originalBookPrice":32761.2153,"bookPrice":32761.2153,"preBookPrice":32761.2153,"contractAmount":0,"inputId":"","inputName":"Selection Name:","inputPrice":0,"inputMode":"ADD","addTotal":0,"bookDifference":0,"userName":"jimk","userType":"Executive","versionNumber":-1,"versionBookPrice":32761.2153,"salesCommission":0}
Maybe reading this cookie with JavaScript is not a good idea. I found that I don't really need this since my projectClose.php will read the cookie and close the project.If it ain't broke don't fix it Discover my world at jkirkerx.com
-
I generated that cookie with PHP 7.4. For some reason it encodes the JSON, but it reads clean with PHP 7.4 I pulled that example out of storage in the browser, and cut off a large section just to use as an example. This is the cookie straight out of the browser storage. Pretty ugly but that's just how it writes even with pretty print.
%7B%22timeStamp%22%3A%2202%5C%2F18%5C%2F2022%2009%3A41%3A35%22%2C%22coreMode%22%3A%22open%22%2C%22projectIntegrityCheck%22%3Afalse%2C%22projectNumber%22%3A%225342%22%2C%22projectType%22%3A%22poolspa%22%2C%22projectStage%22%3A%22designing%22%2C%22projectVersion%22%3A0%2C%22projectFinalVersion%22%3A0%2C%22projectJob%22%3A0%2C%22projectEmployeeId%22%3A91%2C%22projectEmployeeName%22%3A%22Jim%20Miller%22%2C%22projectCustomerId%22%3A%225324%22%2C%22projectCustomerName%22%3A%22Jim%20Kirker%202022021602%22%2C%22projectAddNumber%22%3A-1%2C%22projectSalesId%22%3A%2291%22%2C%22originalBookPrice%22%3A32761.2153%2C%22bookPrice%22%3A32761.2153%2C%22preBookPrice%22%3A32761.2153%2C%22contractAmount%22%3A0%2C%22inputId%22%3A%22%22%2C%22inputName%22%3A%22Selection%20Name%3A%22%2C%22inputPrice%22%3A0%2C%22inputMode%22%3A%22ADD%22%2C%22addTotal%22%3A0%2C%22bookDifference%22%3A0%2C%22userName%22%3A%22jimk%22%2C%22userType%22%3A%22Executive%22%2C%22versionNumber%22%3A-1%2C%22versionBookPrice%22%3A32761.2153%2C%22salesCommission%22%3A0%7D
The same cookie URL decoded ...{"timeStamp":"02\/18\/2022 09:41:35","coreMode":"open","projectIntegrityCheck":false,"projectNumber":"5342","projectType":"poolspa","projectStage":"designing","projectVersion":0,"projectFinalVersion":0,"projectJob":0,"projectEmployeeId":91,"projectEmployeeName":"Jim Miller","projectCustomerId":"5324","projectCustomerName":"Jim Miller 2022021602","projectAddNumber":-1,"projectSalesId":"91","originalBookPrice":32761.2153,"bookPrice":32761.2153,"preBookPrice":32761.2153,"contractAmount":0,"inputId":"","inputName":"Selection Name:","inputPrice":0,"inputMode":"ADD","addTotal":0,"bookDifference":0,"userName":"jimk","userType":"Executive","versionNumber":-1,"versionBookPrice":32761.2153,"salesCommission":0}
Maybe reading this cookie with JavaScript is not a good idea. I found that I don't really need this since my projectClose.php will read the cookie and close the project.If it ain't broke don't fix it Discover my world at jkirkerx.com
Given that string,
JSON.parse
anddecodeURIComponent
work fine:const parsedCookie = JSON.parse(decodeURIComponent("%7B%22timeStamp%22%3A%2202%5C%2F18%5C%2F2022%2009%3A41%3A35%22%2C%22coreMode%22%3A%22open%22%2C%22projectIntegrityCheck%22%3Afalse%2C%22projectNumber%22%3A%225342%22%2C%22projectType%22%3A%22poolspa%22%2C%22projectStage%22%3A%22designing%22%2C%22projectVersion%22%3A0%2C%22projectFinalVersion%22%3A0%2C%22projectJob%22%3A0%2C%22projectEmployeeId%22%3A91%2C%22projectEmployeeName%22%3A%22Jim%20Miller%22%2C%22projectCustomerId%22%3A%225324%22%2C%22projectCustomerName%22%3A%22Jim%20Kirker%202022021602%22%2C%22projectAddNumber%22%3A-1%2C%22projectSalesId%22%3A%2291%22%2C%22originalBookPrice%22%3A32761.2153%2C%22bookPrice%22%3A32761.2153%2C%22preBookPrice%22%3A32761.2153%2C%22contractAmount%22%3A0%2C%22inputId%22%3A%22%22%2C%22inputName%22%3A%22Selection%20Name%3A%22%2C%22inputPrice%22%3A0%2C%22inputMode%22%3A%22ADD%22%2C%22addTotal%22%3A0%2C%22bookDifference%22%3A0%2C%22userName%22%3A%22jimk%22%2C%22userType%22%3A%22Executive%22%2C%22versionNumber%22%3A-1%2C%22versionBookPrice%22%3A32761.2153%2C%22salesCommission%22%3A0%7D"));
/*
{ timeStamp: "02/18/2022 09:41:35", coreMode: "open", projectIntegrityCheck: false, projectNumber: "5342", ...
*/I can't see a semi-colon which would be stripped out. :confused:
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Given that string,
JSON.parse
anddecodeURIComponent
work fine:const parsedCookie = JSON.parse(decodeURIComponent("%7B%22timeStamp%22%3A%2202%5C%2F18%5C%2F2022%2009%3A41%3A35%22%2C%22coreMode%22%3A%22open%22%2C%22projectIntegrityCheck%22%3Afalse%2C%22projectNumber%22%3A%225342%22%2C%22projectType%22%3A%22poolspa%22%2C%22projectStage%22%3A%22designing%22%2C%22projectVersion%22%3A0%2C%22projectFinalVersion%22%3A0%2C%22projectJob%22%3A0%2C%22projectEmployeeId%22%3A91%2C%22projectEmployeeName%22%3A%22Jim%20Miller%22%2C%22projectCustomerId%22%3A%225324%22%2C%22projectCustomerName%22%3A%22Jim%20Kirker%202022021602%22%2C%22projectAddNumber%22%3A-1%2C%22projectSalesId%22%3A%2291%22%2C%22originalBookPrice%22%3A32761.2153%2C%22bookPrice%22%3A32761.2153%2C%22preBookPrice%22%3A32761.2153%2C%22contractAmount%22%3A0%2C%22inputId%22%3A%22%22%2C%22inputName%22%3A%22Selection%20Name%3A%22%2C%22inputPrice%22%3A0%2C%22inputMode%22%3A%22ADD%22%2C%22addTotal%22%3A0%2C%22bookDifference%22%3A0%2C%22userName%22%3A%22jimk%22%2C%22userType%22%3A%22Executive%22%2C%22versionNumber%22%3A-1%2C%22versionBookPrice%22%3A32761.2153%2C%22salesCommission%22%3A0%7D"));
/*
{ timeStamp: "02/18/2022 09:41:35", coreMode: "open", projectIntegrityCheck: false, projectNumber: "5342", ...
*/I can't see a semi-colon which would be stripped out. :confused:
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
decodeUriComponent did not occur to me. I'll go ahead and give that a try and finish the module. I may post another question soon about communicating between browser tabs using LocalStorage or opening up some sort of channel communication. The goal is for an inactivity timer on the main browser tab to close other open tabs that feed off the same cookie, so when the cookie is wiped, the other open tabs won't bomb from the missing cookie.
If it ain't broke don't fix it Discover my world at jkirkerx.com