Así que estoy tratando de crear un almacenamiento local para una de mis páginas, para que el usuario no tenga que enviar sus formularios GET seleccionados todo el tiempo cuando carga la página web.
Hice dos variables url (url actual) y defaultUrl (url sin parámetros (atm no usado)), luego pensé que debería configurar localStorage cuando el usuario cierra la pestaña/página ejecutando window.onbeforeunload . Pero, ¿cómo debo crear la redirección? Porque ahora mismo hace un bucle... Y no sé cómo hacerlo correctamente
var url = location.href; //Current page var defaultUrl = location.protocol + '//' + location.host + location.pathname; //without parameters //When page is closed window.onbeforeunload = function() { return localStorage.setItem("url", url); }; //If local storage is set then load it? BUT HOW? Loops right now... if (localStorage.getItem("url") !== null) { var oldUrl = localStorage.getItem("url"); localStorage.removeItem("url"); window.location.replace(oldUrl); }Editar
Me las arreglé para crear una solución para mi problema, con un poco de ayuda de ustedes.
//LocalStorage saves/redirects to url var url = location.href; //Current page var defaultUrl = location.protocol + '//' + location.host + location.pathname; //without parameters //If the user access the default webpage url then redirect (Only if localStorage isn't default page for some reason) if (url === defaultUrl) { if (localStorage.getItem("url") !== null) { var oldUrl = localStorage.getItem("url"); localStorage.removeItem("url"); if (oldUrl !== defaultUrl) { window.location.replace(oldUrl); } } else { localStorage.setItem("url", url); } } else { //If the user followed a link to the webpage or made a GET request then save this for later localStorage.setItem("url", url); } //All GET submits should have this class, due to it resets the LocalStorage so it's ready to be set again on reload $(document).on('click', '.formClickResetUrl', function(event) { if (localStorage.getItem("url") !== null) { localStorage.removeItem("url"); } });En primer lugar, window.onbeforeunload está en desuso.
Cuando está funcionando, su código mantiene efectivamente la clave "url" en localStorage . Después de eliminarItem , navega a removeItem , onbeforeunload de que se active la oldUrl , se agrega "url" nuevamente.
Intenta seguir sin onbeforeunload
if (localStorage.getItem("url") !== null) { var oldUrl = localStorage.getItem("url"); localStorage.removeItem("url"); window.location.replace(oldUrl); } else { localStorage.setItem("url", url); }Revisado:
var url = location.href; //Current page if (location.search.length === 0) { // url without parameters, the default page (i suppose) if (localStorage.getItem("url") !== null && localStorage.getItem('defurl')) { var oldUrl = localStorage.getItem("url"); window.location.replace(oldUrl); } localStorage.setItem('defurl', true); // visited the default page } else if (!localStorage.getItem('defurl')) { // someone copy/paste a url with parameter, redirect to default page window.location.replace(location.pathname); } else { //If the user followed a link to the webpage or made a GET request then save this for later localStorage.setItem("url", url); }No se requiere el manejo de eventos de clic.
Revisado 2, sin redirección para URL con parámetros
var url = location.href; //Current page if (location.search.length === 0) { // url without parameters, the default page (i suppose) if (localStorage.getItem("url") !== null) { var oldUrl = localStorage.getItem("url"); window.location.replace(oldUrl); } } else { //If the user followed a link to the webpage or made a GET request then save this for later localStorage.setItem("url", url); }