Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

79
Vistas
Using Localstorage to welcome returning users

I have this code to trigger a welcome message for returning users using localstorage. But it doesn't work, when I inspected the cookies used, it doesn't even show that the cookie am using is getting stored. This is the code

jQuery(document).ready(function( $ ){
var canClick = 1;
var ga_visits = localStorage.getItem('visits');
if (ga_visits == null) ga_visits = 1;
$("body").click(function () {
    if (canClick==2) {
         switch(ga_visits){
         case 1:
         alert("Welcome to our website");
         visits=parseFloat(ga_visits)+1;
         continue;
         case 2:
         alert("Welcome to back to our website");
         visits=parseFloat(ga_visits)+1;
         continue;
         case 3:
         alert("Welcome to back to our website, You really seem to like our website");
         visits=parseFloat(ga_visits)+1;
         continue;
else {
canClick=canClick+1;
}
});
});

This alert message will show only when it is second click , so that the immersion doesn't break.

almost 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It seems to me that you're trying to set the value of visits directly, instead of to the localStorage. Instead of visits=parseFloat(ga_visits)+1, use localStorage.setItem('visits', parseFloat(ga_visits)+1) to set the localStorage. As for why it's not in the cookies, keep in mind that cookies != localStorage, you should check the localStorage on devtools -> application -> localStorage to check the localStorage of the current site. Also your code seems to be poorly written, not sure if it even runs. I tried to fix it here as well:

jQuery(document).ready(function( $ ){
  var canClick = 1;
  var ga_visits = localStorage.getItem('visits');
  if (ga_visits == null) ga_visits = 1;
  $("body").click(function () {
    if (canClick==2) {
      switch(ga_visits){
        case 1:
        alert("Welcome to our website");
        localStorage.setItem('visits', parseFloat(ga_visits)+1);
        continue;
      case 2:
        alert("Welcome to back to our website");
        localStorage.setItem('visits', parseFloat(ga_visits)+1);
        continue;
      case 3:
        alert("Welcome to back to our website, You really seem to like our website");
        localStorage.setItem('visits', parseFloat(ga_visits)+1);
        continue;
      }
    } else {
      canClick=canClick+1;
    }
  }
});
almost 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda