How to write in cookie an assigned class element



  • Found some articles on working with cookie, they all described some complex cases, and there's little in the end. I don't need this right now, I want to start with a simple:

    How do you put the block in the class, and after rebooting the page, the class should stay?

    $("input").click(function() {
      $(this).siblings('.block').toggleClass('active')
    });
    .block {
      display: block;
      position: relative;
      width: 80%;
      height: 100px;
      margin: 0 auto;
      background: #4DB6AC;
    }
    .block.active {
      background: #FFC107;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    <div class="block"></div>
    <input type='button' value='Переключатель'>

    I also read that cookie retained for one session only,

    1. How to keep them there as long as the user himself Can't clean the browser?
    2. How do you do cookie 24 hours?


  • Question 1.
    When they're installed, they'll have a parameter. expires♪ Then they'll live as long as you tell them, unless the server rewrites.

    If the date is indicated in the past, the dock will be removed immediately. If you don't point out, the cube is considered sedium and the browser closed.

    You can't watch a doll end in js. But you can use the console of the developer. In chrome, it's on the resources.

    You can't ask for endless cuisine, but you can tell them to live a couple of years, that's enough for you.

    Details can be read http://learn.javascript.ru/cookie

    Question 2.
    Something like:

    var now, expirationDate = new Date(), 
        newCookie;
    

    // Страшное число - это один день в миллисекундах.
    expirationDate.setTime(now.getTime() + 246060*1000);

    newCookie = "myCookie=testCookieValue;expires=" + expirationDate.toUTCString();
    document.cookie = newCookie;

    Item 3.
    Cookie needs to be used for the data that the server needs to know. If this information is not needed at all, the server should be used. localStorage or sessionStorage♪ Data sessionStorage There is a browser session. Data localStorage They live until they get killed. I mean the parameter. expirationDate for localStorage No, it can be realized if you want.

    Storage has functions. setItem(key, value) and getItem(key, value) By which you can get and clean up the meanings.

    I mean, to deal with your main task:
    By the time your element is downloaded (onLoad), you need to check the data that are stored in the storage and hang the right class depending on them.
    When you switch the class on the element, you must change the meaning of the vault.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2