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

228
Views
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 answers
Answer question

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 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!