Easy_solution/assets/js/cookies.js

39 lines
1.2 KiB
JavaScript

var application
function getApplication(){
const body = document.getElementsByTagName('body')[0];
application = body.getAttribute('data-application');
}
getApplication();
// Support pour différents systèmes de navigation
if (typeof Turbo !== 'undefined') {
document.addEventListener('turbo:load', getApplication);
document.addEventListener('turbo:render', getApplication);
}
// Support pour les applications SPA qui utilisent l'historique de navigation
window.addEventListener('popstate', getApplication);
window.setCookie = function (cname, cvalue, exdays) {
const d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
let expires = "expires=" + d.toUTCString();
document.cookie = application + "-" + cname + "=" + cvalue + ";" + expires + ";path=/;SameSite=Strict";
}
window.getCookie = function (cname) {
let name = application + "-" + cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
return "";
}