2016-10-03 59 views
1

我在登錄頁面後運行以下腳本。如果用戶第一次訪問該網站,則會顯示以下消息。在初次和後續訪問中使用localStorage顯示消息

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

現在,我想如果它不是他們的第一次訪問,在彈出窗口中顯示不同的消息。 else要走的路,如果是這樣,什麼是正確的語法?

回答

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> 
0

您需要使用getItem()setItem()

if (!localStorage.getItem['done']) { //this will be false for the first visit 
     localStorage.setItem['done'] = 'yes'; //now set the value 
     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" }); 
    } 
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'); 

} 
+0

謝謝!我已經嘗試過,但沒有把之前的其他。我知道我做錯了什麼,只是無法弄清楚什麼。謝謝。 –