阅读使用JavaScript饼干 [英] Read cookies with JavaScript

查看:136
本文介绍了阅读使用JavaScript饼干的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何写/创建的cookie在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=/";

但我怎么能读的JavaScript他们每个人,因为我要填充的文本框下一次用户前来形式?

But how can I read each one of them in JavaScript because I want to populate the text boxes next time the user come to the form?

我也试过,但它不工作:

I have tried this but it does not work:

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

与编辑答案:

我用这个code ...............

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>

和从在window.onload称为像这样的checkCookie()函数

And called the checkCookie() function like so from the window.onload

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

window.onload = initPage;

function initPage() 
{
    checkCookie();

}

// - >

//-->

享受!

推荐答案

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

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

帮助获取Cookie

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

这篇关于阅读使用JavaScript饼干的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆