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

183
Views
localStorage and onload JavaScript

I tried to make a function that displays an alert on the 1st page load <body onload="alertbox()>", and its working on 2nd page load instead of 1st. What should i use instead of "onload" to make it work only on 1st load?

I'm beginner, sorry if it's a dumb question.

var alertboxStatus = localStorage.getItem("alertbox", alertboxStatus);
localStorage.setItem("alertbox", alertboxStatus)

function alertbox()
{
  if (alertboxStatus == "null" || alertboxStatus == "") 
  {
    alert("Alert!");
    alertboxStatus = "displayed";
    localStorage.setItem("alertbox", alertboxStatus);
  }
};

I also tried this: But it's also working on second page load. Am i doing sth wrong with locationStorage.setItem?

document.addEventListener('DOMContentLoaded', function () 
{
  if (alertboxStatus == "null" || alertboxStatus == "") 
  {
    alert("alert");
    alertboxStatus = "displayed";
    localStorage.setItem("alertbox", alertboxStatus);
  }
}, false);

Changing "null" to null worked

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Try this, use null instead of "null"

document.addEventListener('DOMContentLoaded', function () 
{
  if (alertboxStatus == null || alertboxStatus == "") 
  {
    alert("alert");
    alertboxStatus = "displayed";
    localStorage.setItem("alertbox", alertboxStatus);
  }
}, false);
about 4 years ago · Juan Pablo Isaza Report

0

I recommend you check the status before working with it to avoid the issue with null and 'null'. Also it dosn't make a lot of sense to load the status and then save it again, since you don't change it here.

The null issue

'null' == null \\ = false

I think you can simplify it to this

// will always be null or 'displayed'
var alertboxStatus = localStorage.getItem("alertbox");

function alertbox()
{
  if (!alertboxStatus) 
  {
    alert("Alert!");
    alertboxStatus = "displayed";
    localStorage.setItem("alertbox", alertboxStatus);
  }
};
about 4 years ago · Juan Pablo Isaza Report

0

You can retrieve a numeric value in localStorage and assign it to a variable like this:

let nOfPageLoads = +( localStorage.getItem("nLoads") || 1 );

The + converts the retrieved value to a number. If localStorage.nLoads does not exist yet, || 1 sets nOfPageLoads to 1.

So, if the value of nOfPageLoads is 1, you know the page is visited for the first time. Do something if so (alerting or whatever).

After that, increase nOfPageLoads with 1 and set the value of nLoads in localStorage to the new value.

Here is an example of this idea (in Stackblitz, because localStorage is sandboxed in SO-snippets).

about 4 years ago · Juan Pablo Isaza 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!