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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source