2 Replies - 265 Views - Last Post: 11 July 2019 - 10:56 PM

#1 kayut   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 20-January 17

How to set a cookie with JavaScript, which expires after 24 hours

Posted 04 July 2019 - 09:21 AM

How do I run this function, so that the cookie expires after 24 hours?

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=/";
  }



Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: How to set a cookie with JavaScript, which expires after 24 hours

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15109
  • View blog
  • Posts: 60,436
  • Joined: 12-June 08

Re: How to set a cookie with JavaScript, which expires after 24 hours

Posted 04 July 2019 - 10:00 AM

I would imagine you change the variable name from 'days' to 'hours'..
1	function createCookie(name,value,days) {


.. then correct for the math here:
4	      date.setTime(date.getTime()+(days*24*60*60*1000));


.. and passing in you would do '24'.
Was This Post Helpful? 0
  • +
  • -

#3 sagar.chavada   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-April 19

Re: How to set a cookie with JavaScript, which expires after 24 hours

Posted 11 July 2019 - 10:56 PM

My opinion on this topic is you did right coding of the expire cookie in 24 hours might be you did not get any error in your code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1