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

217
Visualizações
Store dark mode in localStorage across web-pages

Salutations! I'm developing a new blog and, as the title suggests, I'm trying to store my Dark Mode style-sheet in localStorage across web-pages. Currently if a user selects Dark Mode, but then refreshes the page or links to another web-page, the site reverts back to Light Mode again. I want it to remember the user's choice of mode (Light or Dark). This is my current JavaScript code:

function swapStylesheet(sheet) {
document.getElementById('swap').setAttribute('href', sheet); 
}

How might I apply localStorage to this code so that the browser remembers which style-sheet the user chose?

Thank you so much!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's fairly easy using localStorage API.

In order to save to local storage:

function swapStylesheet(sheet) {
document.getElementById('swap').setAttribute('href', sheet); 
localStorage.setItem('swap', sheet);
}

To retrive from local storage:

function getLocalStorageSwapStyle(){
let style=localStorage.getItem('swap');
swapStyleSheet(style);
}

You may call getLocalStorageSwapStyle whenever you want. E.g: on page load or some certain event.

More info regarding localStorage API can be found here: mdn

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