How to make it so that in this pop-up window the button "no longer show" saved the user’s cookie that it is no longer necessary to display this window on subsequent visits to the site?

<!-- Модальное Окно --> <div id="overlay"> <div class="popup"> <p>текст</p> <button class="dont-show-me">больше не показывать</button> </div> </div> <script type="text/javascript"> var delay_popup = 1000; setTimeout("document.getElementById('overlay').style.display='block'", delay_popup); </script> 

    1 answer 1

    Here the modal window is initially displayed, and when you click on the button it disappears, at the same time the necessary cookie is set. When you refresh the page, the window, of course, does not appear. If you refresh the page in a minute (the time of the cookie), the cookie will be deleted and the modal window will reappear.

     <html> <head> <meta charset="utf-8"> </head> <body> <!-- Модальное Окно --> <div id="overlay" style="border: 1px solid black;"> <div class="popup"> <p>текст</p> <button id="hide_popup" class="dont-show-me">больше не показывать</button> </div> </div> <script> function getCookie(name) { let cookie_arr = document.cookie.split('; '); let cookie_obj = {}; for (let i=0; i<cookie_arr.length; i++) { let nv = cookie_arr[i].split('='); cookie_obj[nv[0]] = nv[1]; } return cookie_obj[name]; } let overlay_div = document.getElementById('overlay'); if ( getCookie('hide_popup') == 'yes' ) { overlay_div.style.display='none'; } // При нажатии на кнопку ставим cookie, которая будет запрещать показ // модального окна document.getElementById('hide_popup') .addEventListener('click', function() { // Ставим cookie на минуту. var date = new Date(new Date().getTime() + 60 * 1000); document.cookie = "hide_popup=yes; path=/; expires=" + date.toUTCString(); // и сразу же скрываем окно overlay_div.style.display='none'; }); </script> </body> </html> 

    More information about working with cookies on JavaScript can be found here .

    • Thanks, only this example did not work until I replaced each "let" with "var". - Alexander