Reading Cookies Using JavaScript

I know how to write / create cookies in JavaScript ....................................... ..................

//Create the cookies
document.cookie = "Name=" + Name + ";expires=Friday, 31-Dec-2011 12:00:00 GMT; path=/";
document.cookie = "Surname=" + Surname + ";expires=Friday, 31-Dec-2011 12:00:00 GMT; path=/";
document.cookie = "Number=" + Number + ";expires=Friday, 31-Dec-2011 12:00:00 GMT; path=/";
document.cookie = "Email=" + Email + ";expires=Friday, 31-Dec-2011 12:00:00 GMT; path=/";
document.cookie = "Country=" + Country + ";expires=Friday, 31-Dec-2011 12:00:00 GMT; path=/";
document.cookie = "Company=" + Company + ";expires=Friday, 31-Dec-2011 12:00:00 GMT; path=/";
document.cookie = "Title=" + Job + ";expires=Friday, 31-Dec-2011 12:00:00 GMT; path=/";

But how can I read each of them in JavaScript, because I want to fill in the text fields the next time the user logs into the form?

I tried this, but it does not work:

var cookieName = ReadCookie("Name");
document.getElementById('txtName').value = cookieName;

Edit with answer:

I used this code ...

<script type="text/javascript">

function getCookie(c_name)
{
  if (document.cookie.length>0)
  {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        }
     }
     return "";
 }

function checkCookie()
{
    Name = getCookie('Name');
    Surname = getCookie('Surname');
    Email = getCookie('Email');
    Company = getCookie('Company');
    Title = getCookie('Title');

    if (Email!=null && Email!="")
      {
      //Populate the text boxes..................................
      document.FormName.txtName.value = Name;
      document.FormName.txtSurname.value = Surname;
      document.FormName.txtEmail.value = Email;
      document.FormName.txtCompany.value = Company;
      document.FormName.txtjob.value = Title;
      }
   }

</script>

And called the checkCookie () function, as shown in window.onload

<SCRIPT TYPE='text/javascript' LANGUAGE='JavaScript'><!--    //

window.onload = initPage;

function initPage() 
{
    checkCookie();

}

// ->

Enjoy !!

+3
source share
3 answers

From http://w3schools.com/js/js_cookies.asp

set cookie

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

get cookie

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}
+2
source

document.cookie, cookie. .

var cookies = document.cookie.split(';'); // "cookies" will be an array

- > :

var cookieMap = {};
for (var i = 0; i < cookies.length; ++i) {
  cookies[i].replace(/^\s*([^=]+)=(.*)$/, function(_, name, val) {
    cookieMap[name] = unescape(val);
  });
}

cookie "mycookie" :

var mycookieVal = cookieMap.mycookie;

note, - - , . , document.cookie, , ( "=", ) "" ( "=" cookie). "cookieMap" .

0

These are many * better links than w3schools (the worst web link ever made):

Examples obtained from these links:

// sets the cookie cookie1
document.cookie =
 'cookie1=test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'

// sets the cookie cookie2 (cookie1 is *not* overwritten)
document.cookie =
 'cookie2=test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'

// remove cookie2
document.cookie = 'cookie2=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/'

The mozilla help even has a nice cookie library that you can use.

0
source

Source: https://habr.com/ru/post/1747023/


All Articles