Skip to content

Instantly share code, notes, and snippets.

@cahva
Created July 16, 2020 15:27
Show Gist options
  • Save cahva/01aec7938571eefeef53266bb9745dae to your computer and use it in GitHub Desktop.
Save cahva/01aec7938571eefeef53266bb9745dae to your computer and use it in GitHub Desktop.
Safari cookie fix to get around cookie setting problem in an iframe
(function() {
function sc(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = 'expires='+d.toUTCString();
document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/';
}
function gc(cname) {
var name = cname + '=';
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
var i = 0;
for(; i < ca.length; i += 1) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return '';
}
var is_safari = navigator.userAgent.indexOf('Safari') > -1; // safari detection
if (is_safari) {
if (gc('_vsync_cookie') === '') {
sc('_vsync_cookie', 'true', { expires: 365, path: '/' }); // 1 year
window.location.replace('!{accountCookieUrl}');
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment