使用localStorage在初始和后续访问时显示消息

时间:2016-10-03 16:39:41

标签: javascript

我在登录页面后运行以下脚本。如果是用户第一次访问该站点,则会显示以下消息。

<script src="dist/sweetalert.min.js"></script>
<link rel="stylesheet" type="text/css" href="dist/sweetalert.css">
<script>
   if (!localStorage['done']) {
       localStorage['done'] = 'yes';
       swal({   title: "Getting Started",   text: "Tap 'Select a Task' from the top menu to get started or watch the training video.",   type: "info",   confirmButtonText: "Got It!", animation: "slide-from-top" });
   }
</script>

现在,我想在第一次访问ISN时,在弹出窗口中显示不同的消息。是else的方法,如果是,那么正确的语法是什么?

3 个答案:

答案 0 :(得分:1)

是的,可能。你应该尝试过。

var isFirstVisit = localStorage.getItem('_firstVisit'); // get the key

if (isFirstVisit) { // returning visitor 

   localStorage.setItem('_firstVisit', true); // set key

   swal('Welcome back');

} else { // first visit

   swal('Welcome');

}

答案 1 :(得分:0)

else应该可以正常使用

<script src="dist/sweetalert.min.js"></script>
<link rel="stylesheet" type="text/css" href="dist/sweetalert.css">
<script>
   if (!localStorage['done']) {
       localStorage['done'] = 'yes';
       swal({   title: "Getting Started",   text: "Tap 'Select a Task' from the top menu to get started or watch the training video.",   type: "info",   confirmButtonText: "Got It!", animation: "slide-from-top" });
   } else {
     //whatever alert code you want
   }
</script>

答案 2 :(得分:0)

您需要使用martin_fierrogetItem()

setItem()