初学Javascript之cookie篇

<p style=line-height: 150%><FONT color=#3352cc>版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处</FONT><A href="http://xinyistudio.vicp.net/"><FONT color=#3352cc>http://xinyistudio.vicp.net/</FONT></A><FONT color=#3352cc>和作者信息及本声明</FONT> <p style=line-height: 150%>[译者注:将本文的最后示例代码拷贝至文本文件中,更名文件为homepage.htm格式文件,在浏览器中运行,本文以该示例代码讲解] <p style=line-height: 150%>简介 <p style=line-height: 150%>================================================================= <p style=line-height: 150%>问题是我想解决自动访问<A href="http://www.thehungersite.com">http://www.thehungersite.com</A>。这个页面能够限制你每一天只访问它一次(不要忘记在这链接上点击)。现在为止,每次一开始我做的第一件事就是手工选择我的书签加载这个页面。 <p style=line-height: 150%>为什么不为它创建一个简单的脚本程序呢?因为我想让它在Netscape和IE下都能够同样的工作,我开始学习Javascript。 <p style=line-height: 150%>  <p style=line-height: 150%>解决 <p style=line-height: 150%>================================================================= <p style=line-height: 150%>主题思想很简单:创建一个页面并测试一下今天这个页面是否已被加载过,如果没有加载,那就通过它链接到<A href="http://www.thehungersite.com">http://www.thehungersite.com</A>,并且设置这个页面作为浏览器的主页。 <p style=line-height: 150%>获取页面并重定向是很容易的,问题是如何记忆这个页面已被访问过。 <p style=line-height: 150%>因为Javascript没有文件访问的功能,看来我们只能使用cookies了。 <p style=line-height: 150%>Cookies是一个有大小限制的变量,它与一个服务器的域名相关联,默认情况下cookie的生存期是当浏览器关闭时被清空(注意:不是当你离开这个页面的时候),但可以用一个脚本程序改变这种情况,在用户关闭浏览器后使cookies能够存储下来,Netscape在文件中使用所有的Cookie,而IE分别存储每个cookie。此外,不同的浏览器会带来一些意想不到的情况,你必须确定一个用户在它的浏览器设置中是否关闭了cookies。 <p style=line-height: 150%>一切都很好也很妙,只是现在我还未在IE中测试它,调用示例Javascript语句:cookieExpires = "01-APR-" + nLyear + " GMT";document.cookies = cookieName + "=" + cookieValue + ";  expires=" + cookieExpires; <p style=line-height: 150%>然后调用document.write(document.cookie); <p style=line-height: 150%>document.cookie是空的。 <p style=line-height: 150%>------------------------------------------------------------------------------------------------------- <p style=line-height: 150%>在试验和研究了一下上面的示例程序后,会发现: <p style=line-height: 150%>1。你不能够读和显示cookie。如果你想看一下这个cookie你需要指定与它相同的另一个字符串变量,如下:   document.cookie = cookieName + "=" + cookieValue + "; expires=" + cookieExpires;   myvar = cookieName + "=" + cookieValue + "; expires=" + cookieExpires;   document.write(myvar); <p style=line-height: 150%> 2. 浏览器用了不同的日期格式:    Netscape使用"GMT"结束,IE使用“UTC",这是因为它可以更好的构建一个日期,象下面这样:      var expdate = new Date()   cookieExpires.setTime (expdate.getTime() + 1 * (24 * 60 * 60 * 1000)) //+1 day   cookieExpires.toGMTString() <p style=line-height: 150%>   当你显示日期部分   document.write(expdate.getYear() + "" + expdate.getMonth() + "" + expdate.getDate());   对于2000-11-15的日期,在IE中显示为2000/10/15,在Netscape下显示100/10/15(注:已确定是一些较低版本的   Netscape 浏览器的Y2K问题) <p style=line-height: 150%>   示例中看到像下面这样的部分:   if (platform == "Mac") { lastVisit = lastVisit - (24 * 60 * 60 * 1000)   }   但我不可能检测它。 <p style=line-height: 150%>日期对象有getDate和getDay的方法,第二个方法返回在一周中天的索引号。 <p style=line-height: 150%>-------------------------------------------------------------------------------------------------------知道了这些,基本上就没问题了(现在你可以看一下homepage.htm) <p style=line-height: 150%>[译者注:将本文的最后示例代码拷至文本文件中并保存htm格式,然后运行] <p style=line-height: 150%>最后要说明的是,这不仅是一个专用的JS脚本,如果你想将它用在你的web页面上你必须最小程度的使用不同的浏览器测试它并注意它们的版本,许多的脚本程序包含了浏览器类型检测和大量的if...else语句,以处理这样不同。 <p style=line-height: 150%>  <p style=line-height: 150%>示例页面homepage.htm源代码 <p style=line-height: 150%>=============================================================== <p style=line-height: 150%><html><head><title>Homepage</title></head><body><a href=http://www.thehungersite.com/>Manual redirection</a><a href="javascript:ResetCookie()">Cookie reset</a> <p style=line-height: 150%><script language="JavaScript"><!-- var bVisitedToday = false; <p style=line-height: 150%>var lastVisit = GetCookie("lastVisit");if (lastVisit != null) {  lastVisit = 1 * lastVisit;  var lastHere = new Date(lastVisit);    var rightNow = new Date(); <p style=line-height: 150%>  if(lastHere.getYear() == rightNow.getYear()     && lastHere.getMonth() == rightNow.getMonth()     && lastHere.getDate() == rightNow.getDate())  {     bVisitedToday = true;  }} <p style=line-height: 150%>if(bVisitedToday == false){  setLastlastVisitCookie();  window.location="<A href="http://www.thehungersite.com/">http://www.thehungersite.com/</A>"}else{  //window.location="about:blank"} <p style=line-height: 150%>function getCookieVal (offset){  var endstr = document.cookie.indexOf (";", offset);  if (endstr == -1)    endstr = document.cookie.length;  return unescape(document.cookie.substring(offset, endstr));}function GetCookie (name){  var arg = name + "=";  var alen = arg.length;  var clen = document.cookie.length;  var i = 0;  while (i < clen) {    var j = i + alen;    if (document.cookie.substring(i, j) == arg)      return getCookieVal (j);    i = document.cookie.indexOf(" ", i) + 1;    if (i == 0) break;   }  return null;}function SetCookie (name, value){  var argv = SetCookie.arguments;  var argc = SetCookie.arguments.length;  var expires = (argc > 2) ? argv[2] : null;  var path = (argc > 3) ? argv[3] : null;  var domain = (argc > 4) ? argv[4] : null;  var secure = (argc > 5) ? argv[5] : false;  document.cookie = name + "=" + escape (value) +    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +    ((path == null) ? "" : ("; path=" + path)) +    ((domain == null) ? "" : ("; domain=" + domain)) +    ((secure == true) ? "; secure" : "");}function setLastlastVisitCookie (){  var rightNow = new Date();  var expdate = new Date();  expdate.setTime (expdate.getTime() + 1 * (24 * 60 * 60 * 1000)); //+1 day  SetCookie ("lastVisit", rightNow.getTime(), expdate, "/");}function ResetCookie(){  SetCookie("lastVisit", 0, null, "/");}// --></script></body></html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值