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

146
Visualizações
Refresh an image after URL is changd by JavaScript

I have a background image which is randomly selected on session start but can be changed by a button which triggers an onclick function.

selectImg() is run on page load while resetImg() is what is run when the appropriately named reset button is clicked. How can I make this so rather than reloading the whole page it could just refresh the individual element to change the image - preferably without using libraries such as jQuery?

var images = new Array("assets/img/beach.jpg", "assets/img/city.jpg", "assets/img/valley.jpg", "assets/img/hills.jpg", "assets/img/rocky-beach.jpg", "assets/img/trees-lake.jpg", "assets/img/trees.jpg");


function selectImg() {
  if ("headerImage" in sessionStorage) {
    document.getElementById('headerImage').style.backgroundImage = "url(" + sessionStorage.getItem("headerImage") + ")";
  } else {
    var selector = Math.floor(Math.random() * images.length);
    sessionStorage.headerImage = images[selector];
    document.getElementById('headerImage').style.backgroundImage = "url(" + sessionStorage.getItem("headerImage") + ")";
  }
}

function resetImg() {
  sessionStorage.removeItem("headerImage");
  location.reload();
}
about 4 years ago · Juan Pablo Isaza
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