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

149
Visualizações
Refresh page ONCE

I would like to implement a JavaScript code which states this: if the page is loaded completely, refresh the page immediately, but only once. This one works fine:

window.onload = function() {
    if(!window.location.hash) {
        window.location = window.location + '#loaded';
        window.location.reload();
    }
} 

but I don't want hash at the end. Any solution?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If first time will loaded then you have to set the hash ('loaded'). The automatically will reload the page. Second time he will found the hash #loaded in the url and dont reload the page again.

To avoid hash in the url you can use cookie or localStorage.

LocalStorage: In this case take a look to the answer from @uingtea: https://stackoverflow.com/a/71373469/14807111.

window.onload = function() {  
  if(window.location.hash !== '#loaded') {            
    window.location.hash = '#loaded';
    window.location.reload();
  }
} 

With cookie

function setCookie(cname, cvalue, exdays) {
  const d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  let expires = "expires="+ d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}


function getCookie(cname) {
  let name = cname + "=";
  let decodedCookie = decodeURIComponent(document.cookie);
  let ca = decodedCookie.split(';');
  for(let i = 0; i <ca.length; i++) {
    let c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}



window.onload = function() {  
  if(getCookie('loaded')) {            
    setCookie('loaded','true',180);
    window.location.reload();
  }
}

over 4 years ago · Santiago Trujillo Relatório

0

use cookies or localStorage

window.onload = function() {
    if(!localStorage.loaded) {
        localStorage.setItem('loaded', 'yes')
        window.location.reload();
    }
} 
over 4 years ago · Santiago Trujillo 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