当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 初学Javascript之cookie篇(2)

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

初学Javascript之cookie篇(2)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 401 ::
收藏到网摘: n/a

  在试验和研究了一下上面的示例程序后,会发现:

  1。你不能够读和显示cookie。如果你想看一下这个cookie你需要指定与它相同的另一个字符串变量,如下:
   document.cookie = cookieName + "=" + cookieValue + "; expires=" + cookieExpires;
   myvar = cookieName + "=" + cookieValue + "; expires=" + cookieExpires;
   document.write(myvar);

  2. 浏览器用了不同的日期格式:
    Netscape使用"GMT"结束,IE使用“UTC",这是因为它可以更好的构建一个日期,象下面这样:
      var expdate = new Date()
   cookieExpires.setTime (expdate.getTime() + 1 * (24 * 60 * 60 * 1000)) //+1 day
   cookieExpires.toGMTString()

   当你显示日期部分
   document.write(expdate.getYear() + "<br>" + expdate.getMonth() + "<br>" + expdate.getDate());
   对于2000-11-15的日期,在IE中显示为2000/10/15,在Netscape下显示100/10/15(注:已确定是一些较低版本的
   Netscape 浏览器的Y2K问题)

   示例中看到像下面这样的部分:
   if (platform == "Mac") {
   lastVisit = lastVisit - (24 * 60 * 60 * 1000)
   }
   但我不可能检测它。

  日期对象有getDate和getDay的方法,第二个方法返回在一周中天的索引号。

----------------------------------------
  知道了这些,基本上就没问题了(现在你可以看一下homepage.htm)

[译者注:将本文的最后示例代码拷至文本文件中并保存htm格式,然后运行]

  最后要说明的是,这不仅是一个专用的JS脚本,如果你想将它用在你的web页面上你必须最小程度的使用不同的浏览器测试它并注意它们的版本,许多的脚本程序包含了浏览器类型检测和大量的if...else语句,以处理这样不同。

  示例页面homepage.htm源代码

  =======================================