So I'm using the JSON.parse
-method to retrieve specific data from a cookie. However, if the cookie value does not exist, the input ends. Is there anyway to validate if this data is present before attempting to parse?
actual cookie data : "foo1":"1123","foo2":"332","foo3":"773"
//function to retrieve cookie
function getCookie(name) {
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value !== null) ? unescape(value[1]) : null;
}
var cookie = getCookie("cookiename");
var cookieData = JSON.parse(cookie).foo4;
The code above will give the error unexpected end of input and terminate the program.