asp.net-使用JavaScript读取Cookie

前端之家收集整理的这篇文章主要介绍了asp.net-使用JavaScript读取Cookie 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我知道如何用JavaScript编写/创建cookie. ……………..

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

但是,由于我想在用户下次访问表单时填充文本框,如何在JavaScript中读取其中的每一个?

我已经尝试过了,但是没有用:

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

编辑答案:

我用了这个代码………………………………

<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>

然后从window.onload调用checkCookie()函数

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

window.onload = initPage;

function initPage() 
{
    checkCookie();

}

//->

请享用!!

最佳答案
http://w3schools.com/js/js_cookies.asp

设置饼干

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());
}

得到饼干

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 "";
}
原文链接:https://www.f2er.com/html/530671.html

猜你在找的HTML相关文章