-1

Hello I have a script which should add cookie on sliding up when button is clicked. But it doesn't work at all... I tried all but still this is not working :S

Here is my code:

<head>
 <script src="jquery.min.js"></script>
 <script>
  $(document).ready(function(){
   $(".btn1").click(function(){
     $("p").slideUp();
   });
  });
 </script>
 <script>
  function iLoveCookies(){
    days=30; // number of days to keep the cookie
    myDate = new Date();
    myDate.setTime(myDate.getTime()+(days*24*60*60*1000));
    document.cookie = 'cookieName=12345; expires=' + myDate.toGMTString();
  }
 </script>
</head>
<body>
  <p>12345 <button class="btn1" onclick="iLoveCookies()">OK</button></p>

Thanks for any help!

technosaurus
  • 7,401
  • 1
  • 29
  • 50
  • May be, you are checking cookies for local html file in Chrome? I'm asking because your code works in IE, but Chrome doesn't save cookies for local files. Besides, why don't you just add `iLoveCookies()` call to `$(".btn1").click(function(){` instead of assigning two onclick events in two different ways (this doesn't influence the result, just seems more appropriate to me). – neli Aug 17 '14 at 00:12
  • Thank you that helped me. Script its now working! – user3581595 Aug 17 '14 at 10:59

1 Answers1

-1

Have a look at jquery.cookie: https://github.com/js-cookie/js-cookie.

<script type="text/javascript" src="jquery.cookie.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $.cookie('the_cookie', 'the_value'); // default expires: 365
        $.cookie('the_cookie', 'the_value', { expires: 7 }); // expiring in 7 days 
        console.log($.cookie('the_cookie')); // => "the_value"
    });
</script>

P.S.: You need to go through a web server. Opening the html file in the browser won't work.

phedoreanu
  • 2,418
  • 3
  • 24
  • 26
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – fabian Aug 17 '14 at 00:33
  • Thank you. I've updated my answer. – phedoreanu Aug 17 '14 at 00:50