Cookies

can someone please explain to me about cookies. Do I have to create a cookies for every variable that I have in a form?
It cannot just be this easy.


function newCookie(name,value,days) {

 var days = 1;   // the number at the left reflects the number of days for the cookie to last



 if (days) {

   var date = new Date();

   date.setTime(date.getTime()+(days + 1));

   var expires = "; expires="+date.toGMTString(); }

   else var expires = "";

   document.cookie = name+"="+value+expires+"; path=/"; }



function readCookie(name) {

   var nameSG = name + "=";


  if (document.cookie.indexOf(nameSG) == -1)

    return null;



   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(nameSG) == 0) return c.substring(nameSG.length,c.length); }

    return null; }



function eraseCookie(name) {

  newCookie(name,"",1); }




Hi,

A cookie is a small text file that’s stored in your browser which contains some data:

  1. A name-value pair containing the actual data
  2. An expiry date after which it is no longer valid
  3. The domain and path of the server it should be sent to

They are useful for storing state on your users machine (HTTP being a stateless protocol), an example in point being form values.
You can then retrieve this state at a later point in time and do something with it.

I find this article explains it well.

If you want to store the value of many form fields in cookies, I would consider serializing it first.

The standard accepted length of a day in milliseconds is 86400000

Therefore:

date.setTime(date.getTime()+(days + 1));

should be

date.setTime(date.getTime()+(days * 86400000));

You will find some people who would argue that 86400000 is not entirely correct because of leap years and day light savings, chem-trails and alien abductions. Please ignore them because even if you are setting a cookie, its set to UTC time which does not alter in areas where day light savings are observed.

You will also find a small number of programmers who insist on the “Taking account of day light savings”, after a swift phone call to Microsoft’s technical team that cost me nearly £20 on my mobile, the answer came back as I had argued. All time for javascript is taken from the operating system which will automatically observe DST, the operating system takes its time from what it was set and that the OS only counts the ticks from the BIOS clock. If you happen to sync with a web service that provides a time signal from an atomic clock, this makes no difference, the OS still counts the BIOS clocks ticks not the time it is set to.

I hope that helps you understand how M$ clock in the OS works, DST is automatically taken care of and no need to implement a script that accounts for or adds or subtracts DST timings. Cookies wont be affected because of the previously mentioned DST.

In a spread sheet I put the following…


Days in year	| Milliseconds in Year	| Milliseconds based on 365 days (ms / 365)
365		| 31536000000		| 86400000
365.25		| 31557600000		| 86459178.08
366		| 31622400000		| 86636712.33