简体   繁体   中英

localstorage value is changed on page refresh

I am creating an welcomescreen for my html app. and im using a welcomescreen plugin from github. you can check it here https://github.com/valnub/welcomescreen.js

now i want to show welcome screen when localstorage value is 0. and when close button of welcomescreen is clicked i am changing the localstorage value to 1. but on page refresh the localstorage value is again set to 0.

how to do that this is my js file.

 /*jslint browser: true*/ /*global console, Welcomescreen, $*/ // Init method $(document).ready(function () { localStorage.setItem("welscreen", "0"); var welcomeTour = localStorage.getItem("welscreen"); if (welcomeTour == 0) { $(document).ready(function () { var options = { 'bgcolor': '#0da6ec', 'fontcolor': '#fff', 'onOpened': function () { console.log("welcome screen opened"); console.log(welcomeTour); }, 'onClosed': function () { localStorage.setItem("welscreen","1"); var welcomeTour = localStorage.getItem("welscreen"); console.log("welcome screen closed"); console.log(welcomeTour); } }, welcomescreen_slides, welcomescreen; welcomescreen_slides = [ { id: 'slide0', picture: '<div class="tutorialicon">♥</div>', text: 'Welcome to this tutorial. In the <a class="tutorial-next- link" href="#">next steps</a> we will guide you through a manual that will teach you how to use this app.' }, { id: 'slide1', picture: '<div class="tutorialicon">✲</div>', text: 'This is slide 2' }, { id: 'slide2', picture: '<div class="tutorialicon">♫</div>', text: 'This is slide 3' }, { id: 'slide3', picture: '<div class="tutorialicon">☆</div>', text: 'Thanks for reading! Enjoy this app or go to <a class="tutorial-previous-slide" href="#">previous slide</a>.<br><br><a class="tutorial-close-btn" href="#">End Tutorial</a>' } ]; welcomescreen = new Welcomescreen(welcomescreen_slides, options); $(document).on('click', '.tutorial-close-btn', function () { welcomescreen.close(); }); $('.tutorial-open-btn').click(function () { welcomescreen.open(); }); $(document).on('click', '.tutorial-next-link', function (e) { welcomescreen.next(); }); $(document).on('click', '.tutorial-previous-slide', function (e) { welcomescreen.previous(); }); }); }; }); 

Change this:

localStorage.setItem("welscreen", "0"); 
var welcomeTour = localStorage.getItem("welscreen");

to this:

var welcomeTour = localStorage.getItem("welscreen");
if(welcomeTour === undefined || welcomeTour === null) {
   localStorage.setItem("welscreen", "0");
   welcomeTour = "0";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM