Display alert only once

后端 未结 3 1489
说谎
说谎 2020-12-16 05:14

I have this code that shows alert when it\'s 30 minutes before event in calendar but I want to show it only once when user comes to page (in that 30min). Now it shows on eve

相关标签:
3条回答
  • 2020-12-16 05:33

    You can use localStorage (not compatible with older browsers):

    <script type="text/javascript">
        var alerted = localStorage.getItem('alerted') || '';
        if (alerted != 'yes') {
         alert("My alert.");
         localStorage.setItem('alerted','yes');
        }
    </script>
    

    Or you can use cookies, give a look to this answer for a full example code: https://stackoverflow.com/a/21567127/3625883

    0 讨论(0)
  • 2020-12-16 05:38

    Set a cookie when you show the alert. Then, if the cookie is set, you will know not to show the alert again. If it is not set, you know that you haven't shown the alert yet and should do so now.

    You can read about setting cookies in JavaScript here.

    0 讨论(0)
  • 2020-12-16 05:49

    You have to use cookies or localStorage or sessionStorage to do this!

    See the following link on localStorage: http://www.w3schools.com/html/html5_webstorage.asp

    or

    http://www.webdesignerdepot.com/2013/04/how-to-use-local-storage-for-javascript/

    0 讨论(0)
提交回复
热议问题