Search code examples
javascripthtmlcookiesshow-hide

How to hide a div for 24 hours with a cookie


I need to hide this div for 24 hours. put it's not working.

I would like a simple JavaScript code that will allow me to hide a certain div element when clicked for a predefined amount of time.
To be a little more informative, I have a suggestions box that appears when the home page is loaded. What I would like is when the div close button is clicked, it sets a cookie to keep the box div closed for 24 hours (1 day).
Simply said, when the div close button is pressed, the box div is hidden for 24 hours.

Note: I have a javascript code that allows the close button to close the box but it will re-load every refresh.
Link to the project's file (Editor comment: it's not secure to download files from untrusted websites, it's better to wait for the author to edit the question with the code.)


Solution

  • Thank You All The Currect Code IS

    $(document).ready(function() {
    
      // If the 'hide cookie is not set we show the message
      if (!readCookie('hide')) {
        $('#applink').show();
      }else {
        $('#applink').hide();
      }
    
      // Add the event that closes the popup and sets the cookie that tells us to
      // not show it again until one day has passed.
      $('#playstorclose').click(function() {
        $('#applink').hide();
        createCookie('hide', true, 1)
        return false;
      });
    
    });
    
    // ---
    // And some generic cookie logic
    // ---
    function createCookie(name,value,days) {
      if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
      }
      else var expires = "";
      document.cookie = name+"="+value+expires+"; path=/";
    }
    
    function readCookie(name) {
      var nameEQ = name + "=";
      var ca = document.cookie.split(';');
      for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
      }
      return null;
    }
    
    function eraseCookie(name) {
      createCookie(name,"",-1);
    }
    <div class="playstorapp" id="applink" style="text-align: center;margin: auto">
            <a id="playstorclose" href="JavaScript:void(0)">X</a>
            <a class="applink" href="https://play.google.com/store/apps/details?id=com.aradev.net&rdid=com.aradev.net" target="_blank">
                Download
            </a>
        </div>