trying to fix redirect to home bug : replaced cookie by localStorage

This commit is contained in:
Bachir Soussi Chiadmi
2018-04-04 15:13:35 +02:00
parent ca50cab7d7
commit 4529db50a8
4 changed files with 40 additions and 36 deletions
@@ -1,19 +1,21 @@
console.log('history'); console.log('history');
// https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/ // https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/
function setCookie(name, value, days) { // function setCookie(name, value, days) {
var d = new Date; // var d = new Date;
d.setTime(d.getTime() + 24*60*60*1000*days); // d.setTime(d.getTime() + 24*60*60*1000*days);
document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString(); // document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
} // }
// var edlp is provided by edlp_ajax.module file // var edlp is provided by edlp_ajax.module file
if(edlp.redirect){ if(edlp.redirect){
console.log('history redirect'); console.log('history redirect');
document.body.style.visibility = 'hidden'; document.body.style.visibility = 'hidden';
// record current (will be origin) sys_path in cookie // record current (will be origin) sys_path in cookie
var cookiename = "edlp_origin_path"; // var cookiename = "edlp_origin_path";
var cookie_value = edlp.sys_path.replace(/^\//, '') + '~~' + window.location.pathname; // var cookie_value = edlp.sys_path.replace(/^\//, '') + '~~' + window.location.pathname;
setCookie(cookiename, cookie_value, 1); // setCookie(cookiename, cookie_value, 1);
window.localStorage.setItem('edlp_origin_path', edlp.sys_path.replace(/^\//, ''));
window.localStorage.setItem('edlp_origin_url', window.location.pathname);
// redirect to home // redirect to home
window.location.replace(window.location.origin); window.location.replace(window.location.origin);
}else{ }else{
@@ -255,21 +255,21 @@
console.log('theme : initFirstLoad()', window.location); console.log('theme : initFirstLoad()', window.location);
console.log(document.cookie); console.log(document.cookie);
var url = window.location.pathname; var url = window.location.pathname;
var origin_path = getCookie('edlp_origin_path'); // var origin_path = getCookie('edlp_origin_path');
console.log('origin_path', origin_path); // console.log('origin_path', origin_path);
if(origin_path){ var origin_sys_path = window.localStorage.getItem('edlp_origin_path');
var path_elements = origin_path.split('~~'); console.log('origin_sys_path', origin_sys_path);
console.log(path_elements); var origin_url = window.localStorage.getItem('edlp_origin_url');
var sys_path = path_elements[0]; console.log('origin_url', origin_url);
var url = path_elements[1]; if(origin_sys_path){
// create history state // create history state
var state = { var state = {
ajax_path: parseAjaxSysPath(sys_path), ajax_path: parseAjaxSysPath(origin_sys_path),
sys_path: sys_path, sys_path: origin_sys_path,
}; };
console.log('state', state); console.log('state', state);
// record history state // record history state
history.replaceState(state, null, url); history.replaceState(state, null, origin_url);
// load content through ajax // load content through ajax
ajaxLoadContent(null, state.sys_path, state.ajax_path); ajaxLoadContent(null, state.sys_path, state.ajax_path);
// reset the cookie // reset the cookie
@@ -1,19 +1,21 @@
console.log('history'); console.log('history');
// https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/ // https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/
function setCookie(name, value, days) { // function setCookie(name, value, days) {
var d = new Date; // var d = new Date;
d.setTime(d.getTime() + 24*60*60*1000*days); // d.setTime(d.getTime() + 24*60*60*1000*days);
document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString(); // document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
} // }
// var edlp is provided by edlp_ajax.module file // var edlp is provided by edlp_ajax.module file
if(edlp.redirect){ if(edlp.redirect){
console.log('history redirect'); console.log('history redirect');
document.body.style.visibility = 'hidden'; document.body.style.visibility = 'hidden';
// record current (will be origin) sys_path in cookie // record current (will be origin) sys_path in cookie
var cookiename = "edlp_origin_path"; // var cookiename = "edlp_origin_path";
var cookie_value = edlp.sys_path.replace(/^\//, '') + '~~' + window.location.pathname; // var cookie_value = edlp.sys_path.replace(/^\//, '') + '~~' + window.location.pathname;
setCookie(cookiename, cookie_value, 1); // setCookie(cookiename, cookie_value, 1);
window.localStorage.setItem('edlp_origin_path', edlp.sys_path.replace(/^\//, ''));
window.localStorage.setItem('edlp_origin_url', window.location.pathname);
// redirect to home // redirect to home
window.location.replace(window.location.origin); window.location.replace(window.location.origin);
}else{ }else{
@@ -255,21 +255,21 @@
console.log('theme : initFirstLoad()', window.location); console.log('theme : initFirstLoad()', window.location);
console.log(document.cookie); console.log(document.cookie);
var url = window.location.pathname; var url = window.location.pathname;
var origin_path = getCookie('edlp_origin_path'); // var origin_path = getCookie('edlp_origin_path');
console.log('origin_path', origin_path); // console.log('origin_path', origin_path);
if(origin_path){ var origin_sys_path = window.localStorage.getItem('edlp_origin_path');
var path_elements = origin_path.split('~~'); console.log('origin_sys_path', origin_sys_path);
console.log(path_elements); var origin_url = window.localStorage.getItem('edlp_origin_url');
var sys_path = path_elements[0]; console.log('origin_url', origin_url);
var url = path_elements[1]; if(origin_sys_path){
// create history state // create history state
var state = { var state = {
ajax_path: parseAjaxSysPath(sys_path), ajax_path: parseAjaxSysPath(origin_sys_path),
sys_path: sys_path, sys_path: origin_sys_path,
}; };
console.log('state', state); console.log('state', state);
// record history state // record history state
history.replaceState(state, null, url); history.replaceState(state, null, origin_url);
// load content through ajax // load content through ajax
ajaxLoadContent(null, state.sys_path, state.ajax_path); ajaxLoadContent(null, state.sys_path, state.ajax_path);
// reset the cookie // reset the cookie