Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!