Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda