Значение localStorage сохраняется и загружается правильно, но не применяется к вводу флажка

Я пытаюсь использовать localStorage для хранения и загрузки проверенного состояния флажка. Значения правильно сохраняются и извлекаются из localStorage, но флажок не отражает эти значения при загрузке страницы.

Вот краткое описание моего кода:

const driver = window.driver.js.driver;

const roleBasedSteps = {
    admin: {
        commonSteps: [
            {
                element: '#themeSwitchBtn',
                popover: {
                    title: 'Switch Theme',
                    description: `<p>Swtich between light and dark mode.</p>
                    <label><input type='checkbox' value='' class='startGuideOnLogin'> Always start guide on login</label>`,
                    position: 'auto'
                    // side: 'top', // left, top, right, bottom
                    // align: 'start' // start, center, end
                }
            },
            {
                element: '#navProfileBtn',
                popover: {
                    title: 'More Options',
                    description: 'More options to handle your profile.',
                    position: 'auto'
                }
            },
            {
                element: '#dashboardBtn',
                popover: {
                    title: 'Dashbaord',
                    description: 'Overview your overall progress in figures, charts and tables.',
                    position: 'auto'
                }
            },
            {
                element: '#emailBtn',
                popover: {
                    title: 'Email',
                    description: 'Email box where you can send, receive and read emails.',
                    position: 'auto'
                }
            },
            {
                element: '#usersDropdown',
                popover: {
                    title: 'Users List',
                    description: 'View all users list to manage them.',
                    position: 'auto'
                }
            }
        ],
        'index.php': [
            {
                element: '#adminDashboardCountStats',
                popover: {
                    title: 'Total Counts',
                    description: 'View all counts of meetings, clients, client interviews and placed clients.',
                    position: 'auto'
                }
            }
        ]
    },
    employee: {
        commonSteps: [
            {
                element: '#themeSwitchBtn',
                popover: {
                    title: 'Switch Theme',
                    description: 'Swtich between light and dark mode.',
                    position: 'auto'
                }
            },
            {
                element: '#navProfileBtn',
                popover: {
                    title: 'More Options',
                    description: 'More options to handle your profile.',
                    position: 'auto'
                }
            },
            {
                element: '#dashboardBtn',
                popover: {
                    title: 'Dashbaord',
                    description: 'Overview your overall progress in figures, charts and tables.',
                    position: 'auto'
                }
            },
            {
                element: '#emailBtn',
                popover: {
                    title: 'Email',
                    description: 'Email box where you can send, receive and read emails.',
                    position: 'auto'
                }
            },
            {
                element: '#usersDropdown',
                popover: {
                    title: 'Users List',
                    description: 'View all users list to manage them.',
                    position: 'auto'
                }
            }
        ],
        'index.php': [
            {
                element: '#employeeDashboardCountStats',
                popover: {
                    title: 'Total Counts',
                    description: 'View all counts of meetings, clients, client interviews and placed clients.',
                    position: 'auto'
                }
            }
        ]
    },
    candidate: {
        commonSteps: [
            {
                element: '#themeSwitchBtn',
                popover: {
                    title: 'Switch Theme',
                    description: 'Swtich between light and dark mode.',
                    position: 'auto'
                }
            },
            {
                element: '#navProfileBtn',
                popover: {
                    title: 'More Options',
                    description: 'More options to handle your profile.',
                    position: 'auto'
                }
            },
            {
                element: '#dashboardBtn',
                popover: {
                    title: 'Dashbaord',
                    description: 'Overview your overall progress in figures, charts and tables.',
                    position: 'auto'
                }
            },
            {
                element: '#emailBtn',
                popover: {
                    title: 'Email',
                    description: 'Email box where you can send, receive and read emails.',
                    position: 'auto'
                }
            },
            {
                element: '#jobsListBtn',
                popover: {
                    title: 'Jobs List',
                    description: 'View all listed jobs where you can check and give feedback to us.',
                    position: 'auto'
                }
            },
            {
                element: '#kanbanDropdown',
                popover: {
                    title: 'Interviews Board',
                    description: 'Mnanage your interviews and their progress.',
                    position: 'auto'
                }
            },
            {
                element: '#accountDropdown',
                popover: {
                    title: 'Account',
                    description: 'Manage your profile.',
                    position: 'auto'
                }
            },
            {
                element: '#calendarBtn',
                popover: {
                    title: 'Calendar',
                    description: 'View calendar and events.',
                    position: 'auto'
                }
            },
            {
                element: '#collapseBtn',
                popover: {
                    title: 'Collapse Sidebar',
                    description: 'Collapse sidebar menu.',
                    position: 'auto'
                }
            }
        ],
        'index.php': [
            {
                element: '#dashboardTotalCounts',
                popover: {
                    title: 'Total Counts',
                    description: 'View all counts of jobs, interviews and meetings.',
                    position: 'auto'
                }
            },
            {
                element: '#dashboardAppliedJobsCount',
                popover: {
                    title: 'Applied Jobs Graph',
                    description: 'View graphical representation below based on selected date range of applied jobs.',
                    position: 'auto'
                }
            },
            {
                element: '#dashboardInterviewsListTable',
                popover: {
                    title: 'Interviews List Table',
                    description: 'View tabular view of your interviews.',
                    position: 'auto'
                }
            }
        ],
        'email.php': [
            {
                element: '.composeEmailBtn',
                popover: {
                    title: 'Compose Email',
                    description: 'Opens compose emails section.',
                    position: 'auto'
                }
            },
            {
                element: '#emailSidebarSection',
                popover: {
                    title: 'Email Folders',
                    description: 'View emails in each folder.',
                    position: 'auto'
                }
            },
            {
                element: '#load-folder-emails',
                popover: {
                    title: 'Emails List',
                    description: 'View all emails of specific folder.',
                    position: 'auto'
                }
            }
        ],
        'jobs_list.php': [
            {
                element: '#jobsListPageSearch',
                popover: {
                    title: 'Search Job',
                    description: 'Search from jobs list.',
                    position: 'auto'
                }
            },
            {
                element: '#jobsListAction',
                popover: {
                    title: 'Give Rating',
                    description: 'Rate any job by clicking action button against the job.',
                    position: 'auto'
                }
            },
            {
                element: '#jobsListAction',
                popover: {
                    title: 'Give Review',
                    description: 'Review any job by clicking action button against the job.',
                    position: 'auto'
                }
            }
        ],
        'kanban.php': [
            {
                element: '#kanbanInterviews',
                popover: {
                    title: 'Pending Interviews',
                    description: 'Here is the list of all upcoming interviews.',
                    position: 'auto'
                }
            },
            {
                element: '#kanbanStage1',
                popover: {
                    title: 'First Stage',
                    description: 'Here is the list of interviews of first stage.',
                    position: 'auto'
                }
            },
            {
                element: '#kanbanStage2',
                popover: {
                    title: 'Second Stage',
                    description: 'Here is the list of interviews of second stage.',
                    position: 'auto'
                }
            },
            {
                element: '#kanbanRejected',
                popover: {
                    title: 'Rejected',
                    description: 'Here is the list of failed interviews.',
                    position: 'auto'
                }
            },
            {
                element: '#kanbanOffer',
                popover: {
                    title: 'Offer',
                    description: 'Here is the list of passed interviews.',
                    position: 'auto'
                }
            },
            {
                element: '.kanbanAddInterview1',
                popover: {
                    title: 'Add New',
                    description: 'Opens pop up to add new item.',
                    position: 'auto'
                }
            },
            {
                element: '.kanbanAddInterview2',
                popover: {
                    title: 'Add New',
                    description: 'Opens pop up to add new item.',
                    position: 'auto'
                }
            },
            {
                element: '.kanbanAddInterview3',
                popover: {
                    title: 'Add New',
                    description: 'Opens pop up to add new item.',
                    position: 'auto'
                }
            },
            {
                element: '.kanbanAddInterview4',
                popover: {
                    title: 'Add New',
                    description: 'Opens pop up to add new item.',
                    position: 'auto'
                }
            },
            {
                element: '.kanbanAddInterview5',
                popover: {
                    title: 'Add New',
                    description: 'Opens pop up to add new item.',
                    position: 'auto'
                }
            }
        ]
    }
};

function mergeGuideSteps(common, specific) {
    return [...common, ...specific];
}

const steps = mergeGuideSteps(roleBasedSteps[userRole].commonSteps, roleBasedSteps[userRole][currentPage]) || [];

function expandHiddenAreas(steps) {
    steps.forEach(step => {
        const element = document.querySelector(step.element);
        if (element) {
            let parent = element.parentElement;
            while (parent) {
                if (parent.style.display === 'none' || parent.style.visibility === 'hidden') {
                    parent.style.display = '';
                    parent.style.visibility = '';
                }
                if (parent.classList.contains('collapsed')) {
                    parent.classList.remove('collapsed');
                }
                parent = parent.parentElement;
            }
        }
    });
}

function isElementInCommonSteps(commonSteps, element) {
    const result = commonSteps.some(step => step.element === element);
    // console.info(`Checking if element ${element} is in common steps: ${result}`);
    return result;
}

function expandSidebarIfNeeded(commonSteps, currentElement) {
    const sidebar = document.querySelector('#navbarVerticalCollapse');
    const isMobileScreen = window.innerWidth <= 990;
    // console.info(window.innerWidth);

    const cElement = "#" + currentElement;

    if (isMobileScreen && isElementInCommonSteps(commonSteps, cElement)) {
        sidebar.classList.add('show');
    } else {
        sidebar.classList.remove('show');
    }
}

const driverObj = driver({
    showProgress: true,
    steps: steps,
    onHighlightStarted: (element) => {
        expandSidebarIfNeeded(roleBasedSteps[userRole].commonSteps, element.id);
    }
});

document.addEventListener('click', function (event) {
    if (event.target && event.target.classList.contains('startGuideOnLogin')) {
        const checked = '1';
        const unchecked = '0';

        if (event.target.value === '0' || event.target.value === '') {
            localStorage.setItem('startGuideOnLogin', checked);
            localStorage.setItem('startGuideBox', 'true');
            event.target.value = checked;
            event.target.checked = true;
        } else {
            localStorage.setItem('startGuideOnLogin', unchecked);
            localStorage.setItem('startGuideBox', 'false');
            event.target.value = unchecked;
            event.target.checked = false;
        }

        console.info('Checkbox value:', event.target.value);
    }
});

document.getElementById('startGuide').addEventListener('click', function () {
    expandHiddenAreas(steps);
    driverObj.drive();
});

window.addEventListener('load', function () {
    const isStartGuide = JSON.parse(localStorage.getItem('startGuideOnLogin'));
    const isBoxChecked = JSON.parse(localStorage.getItem('startGuideBox'));

    console.info('Start Guide: ' + isStartGuide);
    console.info('Input Box: ' + isBoxChecked);

    const checkbox = document.querySelectorAll('.startGuideOnLogin');

    if (checkbox) {
        checkbox.value = isStartGuide;
        checkbox.checked = isBoxChecked;
        
        console.info("values: " + checkbox.value + ", " + checkbox.checked);
    }

    if (isStartGuide === 1) {
        expandHiddenAreas(steps);
        driverObj.drive();
    }
});

Проблема:

  1. Значения правильно сохраняются в localStorage, и их можно увидеть, когда я проверяю localStorage в инструментах разработчика браузера.
  2. Однако при загрузке страницы флажок не отражает проверенное состояние на основе значений, хранящихся в localStorage.

Предпринятые шаги по устранению неполадок:

  1. Проверьте значения localStorage с помощью console.info.
  2. Проверил консоль JavaScript на наличие ошибок.
  3. Убедитесь, что флажок имеет правильный класс и присутствует в DOM при запуске сценария.

Вопрос:

В чем может быть причина того, что состояние флажка не применяется, несмотря на то, что значения localStorage правильно сохраняются и загружаются? Как я могу решить эту проблему?

Поведение ключевого слова "this" в стрелочной функции в сравнении с нормальной функцией
Поведение ключевого слова "this" в стрелочной функции в сравнении с нормальной функцией
В JavaScript одним из самых запутанных понятий является поведение ключевого слова "this" в стрелочной и обычной функциях.
Концепция локализации и ее применение в приложениях React ⚡️
Концепция локализации и ее применение в приложениях React ⚡️
Локализация - это процесс адаптации приложения к различным языкам и культурным требованиям. Это позволяет пользователям получить опыт, соответствующий...
Улучшение производительности загрузки с помощью Google Tag Manager и атрибута Defer
Улучшение производительности загрузки с помощью Google Tag Manager и атрибута Defer
В настоящее время производительность загрузки веб-сайта имеет решающее значение не только для удобства пользователей, но и для ранжирования в...
Безумие обратных вызовов в javascript [JS]
Безумие обратных вызовов в javascript [JS]
Здравствуйте! Юный падаван 🚀. Присоединяйся ко мне, чтобы разобраться в одной из самых запутанных концепций, когда вы начинаете изучать мир...
Система управления парковками с использованием HTML, CSS и JavaScript
Система управления парковками с использованием HTML, CSS и JavaScript
Веб-сайт по управлению парковками был создан с использованием HTML, CSS и JavaScript. Это простой сайт, ничего вычурного. Основная цель -...
JavaScript Вопросы с множественным выбором и ответы
JavaScript Вопросы с множественным выбором и ответы
Если вы ищете платформу, которая предоставляет вам бесплатный тест JavaScript MCQ (Multiple Choice Questions With Answers) для оценки ваших знаний,...
1
0
52
2
Перейти к ответу Данный вопрос помечен как решенный

Ответы 2

Вы можете попытаться указать фактическое значение boolean для checkbox.checked вместо 0 или 1. Вы можете сделать это с помощью

Или

Используя приведение типов Boolean:

/*
* Existing code
*/
if (checkbox) {
    checkbox.value = isStartGuide;
    checkbox.checked = Boolean(isBoxChecked);
/*
* Remaining existing code.
*/

ИЛИ

Использование тернарного оператора для определения состояния checkbox:

/*
* Existing Code
*/
if (checkbox) {
    checkbox.value = isStartGuide;
    checkbox.checked = isBoxChecked == 1 ? true : false;
/*
* Remaining Existing Code.
*/

PS, пожалуйста, используйте только один из вышеперечисленных обходных путей.

Пробовал ваши методы, но они не сработали. Я вижу значения в консоли, но эти значения не влияют на флажок.

Muhammad Aleem 19.07.2024 13:43

Вы проверили, что поток управления входит в тело if? Проверяли ли вы значение внутри блока if перед его назначением/типированием?

Mohsin Murad 19.07.2024 14:13
Ответ принят как подходящий

Я отладил проблему и обнаружил, что из-за динамической загрузки DOM для всплывающих подсказок состояние флажка не было доступно и не установлено по желанию. Я использовал MutationObserver, чтобы определить, когда поповер добавляется в DOM, и это устранило мою проблему. Вот мой обновленный код, который теперь работает как задумано:

const driver = window.driver.js.driver;

const roleBasedSteps = {
  admin: {
    commonSteps: [
    {
      element: '#themeSwitchBtn',
      popover: {
        title: 'Switch Theme',
        description: `<p>Swtich between light and dark mode.</p>
                    <input type='checkbox' class='startGuideOnLogin'>&nbsp;<label>Always start guide on login</label>`,
        position: 'auto'
        // side: 'top', // left, top, right, bottom
        // align: 'start' // start, center, end
      }
    },
    {
      element: '#navProfileBtn',
      popover: {
        title: 'More Options',
        description: 'More options to handle your profile.',
        position: 'auto'
      }
    },
    {
      element: '#dashboardBtn',
      popover: {
        title: 'Dashbaord',
        description: 'Overview your overall progress in figures, charts and tables.',
        position: 'auto'
      }
    },
    {
      element: '#emailBtn',
      popover: {
        title: 'Email',
        description: 'Email box where you can send, receive and read emails.',
        position: 'auto'
      }
    },
    {
      element: '#usersDropdown',
      popover: {
        title: 'Users List',
        description: 'View all users list to manage them.',
        position: 'auto'
      }
    }],
    'index.php': [
    {
      element: '#adminDashboardCountStats',
      popover: {
        title: 'Total Counts',
        description: 'View all counts of meetings, clients, client interviews and placed clients.',
        position: 'auto'
      }
    }]
  },
  employee: {
    commonSteps: [
    {
      element: '#themeSwitchBtn',
      popover: {
        title: 'Switch Theme',
        description: `<p>Swtich between light and dark mode.</p>
                    <input type='checkbox' class='startGuideOnLogin'>&nbsp;<label>Always start guide on login</label>`,
        position: 'auto'
      }
    },
    {
      element: '#navProfileBtn',
      popover: {
        title: 'More Options',
        description: 'More options to handle your profile.',
        position: 'auto'
      }
    },
    {
      element: '#dashboardBtn',
      popover: {
        title: 'Dashbaord',
        description: 'Overview your overall progress in figures, charts and tables.',
        position: 'auto'
      }
    },
    {
      element: '#emailBtn',
      popover: {
        title: 'Email',
        description: 'Email box where you can send, receive and read emails.',
        position: 'auto'
      }
    },
    {
      element: '#usersDropdown',
      popover: {
        title: 'Users List',
        description: 'View all users list to manage them.',
        position: 'auto'
      }
    }],
    'index.php': [
    {
      element: '#employeeDashboardCountStats',
      popover: {
        title: 'Total Counts',
        description: 'View all counts of meetings, clients, client interviews and placed clients.',
        position: 'auto'
      }
    }]
  },
  candidate: {
    commonSteps: [
    {
      element: '#themeSwitchBtn',
      popover: {
        title: 'Switch Theme',
        description: `<p>Swtich between light and dark mode.</p>
                    <input type='checkbox' class='startGuideOnLogin'>&nbsp;<label>Always start guide on login</label>`,
        position: 'auto'
      }
    },
    {
      element: '#navProfileBtn',
      popover: {
        title: 'More Options',
        description: 'More options to handle your profile.',
        position: 'auto'
      }
    },
    {
      element: '#dashboardBtn',
      popover: {
        title: 'Dashbaord',
        description: 'Overview your overall progress in figures, charts and tables.',
        position: 'auto'
      }
    },
    {
      element: '#emailBtn',
      popover: {
        title: 'Email',
        description: 'Email box where you can send, receive and read emails.',
        position: 'auto'
      }
    },
    {
      element: '#jobsListBtn',
      popover: {
        title: 'Jobs List',
        description: 'View all listed jobs where you can check and give feedback to us.',
        position: 'auto'
      }
    },
    {
      element: '#kanbanDropdown',
      popover: {
        title: 'Interviews Board',
        description: 'Mnanage your interviews and their progress.',
        position: 'auto'
      }
    },
    {
      element: '#accountDropdown',
      popover: {
        title: 'Account',
        description: 'Manage your profile.',
        position: 'auto'
      }
    },
    {
      element: '#calendarBtn',
      popover: {
        title: 'Calendar',
        description: 'View calendar and events.',
        position: 'auto'
      }
    },
    {
      element: '#collapseBtn',
      popover: {
        title: 'Collapse Sidebar',
        description: 'Collapse sidebar menu.',
        position: 'auto'
      }
    }],
    'index.php': [
    {
      element: '#dashboardTotalCounts',
      popover: {
        title: 'Total Counts',
        description: 'View all counts of jobs, interviews and meetings.',
        position: 'auto'
      }
    },
    {
      element: '#dashboardAppliedJobsCount',
      popover: {
        title: 'Applied Jobs Graph',
        description: 'View graphical representation below based on selected date range of applied jobs.',
        position: 'auto'
      }
    },
    {
      element: '#dashboardInterviewsListTable',
      popover: {
        title: 'Interviews List Table',
        description: 'View tabular view of your interviews.',
        position: 'auto'
      }
    }],
    'email.php': [
    {
      element: '.composeEmailBtn',
      popover: {
        title: 'Compose Email',
        description: 'Opens compose emails section.',
        position: 'auto'
      }
    },
    {
      element: '#emailSidebarSection',
      popover: {
        title: 'Email Folders',
        description: 'View emails in each folder.',
        position: 'auto'
      }
    },
    {
      element: '#load-folder-emails',
      popover: {
        title: 'Emails List',
        description: 'View all emails of specific folder.',
        position: 'auto'
      }
    }],
    'jobs_list.php': [
    {
      element: '#jobsListPageSearch',
      popover: {
        title: 'Search Job',
        description: 'Search from jobs list.',
        position: 'auto'
      }
    },
    {
      element: '#jobsListAction',
      popover: {
        title: 'Give Rating',
        description: 'Rate any job by clicking action button against the job.',
        position: 'auto'
      }
    },
    {
      element: '#jobsListAction',
      popover: {
        title: 'Give Review',
        description: 'Review any job by clicking action button against the job.',
        position: 'auto'
      }
    }],
    'kanban.php': [
    {
      element: '#kanbanInterviews',
      popover: {
        title: 'Pending Interviews',
        description: 'Here is the list of all upcoming interviews.',
        position: 'auto'
      }
    },
    {
      element: '#kanbanStage1',
      popover: {
        title: 'First Stage',
        description: 'Here is the list of interviews of first stage.',
        position: 'auto'
      }
    },
    {
      element: '#kanbanStage2',
      popover: {
        title: 'Second Stage',
        description: 'Here is the list of interviews of second stage.',
        position: 'auto'
      }
    },
    {
      element: '#kanbanRejected',
      popover: {
        title: 'Rejected',
        description: 'Here is the list of failed interviews.',
        position: 'auto'
      }
    },
    {
      element: '#kanbanOffer',
      popover: {
        title: 'Offer',
        description: 'Here is the list of passed interviews.',
        position: 'auto'
      }
    },
    {
      element: '.kanbanAddInterview1',
      popover: {
        title: 'Add New',
        description: 'Opens pop up to add new item.',
        position: 'auto'
      }
    },
    {
      element: '.kanbanAddInterview2',
      popover: {
        title: 'Add New',
        description: 'Opens pop up to add new item.',
        position: 'auto'
      }
    },
    {
      element: '.kanbanAddInterview3',
      popover: {
        title: 'Add New',
        description: 'Opens pop up to add new item.',
        position: 'auto'
      }
    },
    {
      element: '.kanbanAddInterview4',
      popover: {
        title: 'Add New',
        description: 'Opens pop up to add new item.',
        position: 'auto'
      }
    },
    {
      element: '.kanbanAddInterview5',
      popover: {
        title: 'Add New',
        description: 'Opens pop up to add new item.',
        position: 'auto'
      }
    }]
  }
};

function mergeGuideSteps(common, specific) {
  return [...common, ...specific];
}

const steps = mergeGuideSteps(roleBasedSteps[userRole].commonSteps, roleBasedSteps[userRole][currentPage]) || [];

function expandHiddenAreas(steps) {
  steps.forEach(step => {
    const element = document.querySelector(step.element);
    if (element) {
      let parent = element.parentElement;
      while (parent) {
        if (parent.style.display === 'none' || parent.style.visibility === 'hidden') {
          parent.style.display = '';
          parent.style.visibility = '';
        }
        if (parent.classList.contains('collapsed')) {
          parent.classList.remove('collapsed');
        }
        parent = parent.parentElement;
      }
    }
  });
}

function isElementInCommonSteps(commonSteps, element) {
  const result = commonSteps.some(step => step.element === element);
  // console.info(`Checking if element ${element} is in common steps: ${result}`);
  return result;
}

function expandSidebarIfNeeded(commonSteps, currentElement) {
  const sidebar = document.querySelector('#navbarVerticalCollapse');
  const isMobileScreen = window.innerWidth <= 990;
  // console.info(window.innerWidth);

  const cElement = "#" + currentElement;

  if (isMobileScreen && isElementInCommonSteps(commonSteps, cElement)) {
    sidebar.classList.add('show');
  } else {
    sidebar.classList.remove('show');
  }
}

const driverObj = driver({
  showProgress: true,
  steps: steps,
  onHighlightStarted: (element) => {
    expandSidebarIfNeeded(roleBasedSteps[userRole].commonSteps, element.id);
  }
});

document.getElementById('startGuide').addEventListener('click', function() {
  expandHiddenAreas(steps);
  driverObj.drive();
});

// Retrieve and set checkbox state
function setCheckboxState() {
  const storedValue = localStorage.getItem('startGuideOnLogin');
  const checkbox = document.querySelector('.startGuideOnLogin');
  if (checkbox) {
    // Default value is checked if no value is stored
    checkbox.checked = storedValue === null ? true : storedValue === '1';

    checkbox.addEventListener('change', function() {
      localStorage.setItem('startGuideOnLogin', this.checked ? '1' : '0');
    });
  }
}

// Attach event listener only once after document is loaded
document.addEventListener('DOMContentLoaded', function() {
  const observer = new MutationObserver(() => {
    setCheckboxState();
  });

  observer.observe(document.body, { childList: true, subtree: true });
});

window.addEventListener('load', function() {
  let isStartGuide = localStorage.getItem('startGuideOnLogin');

  // If the value is not present in localStorage, set it to the default value '1'
  if (isStartGuide === null) {
    isStartGuide = '1';
    localStorage.setItem('startGuideOnLogin', isStartGuide);
  }

  // Convert the string to a number
  isStartGuide = parseInt(isStartGuide, 10);

  if (isStartGuide === 1) {
    expandHiddenAreas(steps);
    driverObj.drive();
  }
});

Другие вопросы по теме