Was wondering how long it takes to write to document.cookie. Ran into an edge case when setting a cookie and re-directing to another page and the document.cookie was not getting set. Seemed to have different performance on a desktop (Chrome, firefox) vs iphone/tablet (safari)
Seemed to worked correctly in all cases when I added a set timeout of about 500ms
// writing cookie out
function set_cookie(name, value ) {
var date = new Date();
date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
var cookie = name + "=" + value + "; expires=" + date.toGMTString() + ";";
document.cookie = cookie;
}
// reading cookie
function read_Cookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) {
var cVal = c.substring(nameEQ.length, c.length);
return cVal;
}
}
return null;
}
// button click setting cookie and navigation
$("#goToWebButton").click(function() {
if ($('#chkDontShow').attr('checked') == 'checked') {
set_cookie('IgnoreInAppLink','web');
}
setTimeout(function(){window.location.href = '';}, 500);
});
$("#goToAppButton").click(function() {
if ($('#chkDontShow').attr('checked') == 'checked') {
set_cookie('IgnoreInAppLink','app');
}
setTimeout(function(){window.location.href = '';},500);
});