// local storage
let theme = 'dark';
let lang = 'en';
// 1. Могут быть два варианта: сохранять переменную при каждом её изменении, или
//сохранить данные только перед перезагрузкой или закрытием страницы(событие beforeunload)
function setLocalStorage() {
localStorage.setItem('lang', lang);
localStorage.setItem('theme', theme);
}
window.addEventListener('beforeunload', setLocalStorage);
// 2. перед загрузкой страницы (событие load) данные нужно восстановить и отобразить
function getLocalStorage() {
if (localStorage.getItem('lang')) {
const lang = localStorage.getItem('lang');
getTranslate(lang);
};
if (localStorage.getItem('theme')) {
const theme = localStorage.getItem('theme');
if (theme === 'light') {
changeTheme();
}
}
}
window.addEventListener('load', getLocalStorage);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter