初学Javascript之cookie篇(译)
最后要说明的是,这不仅是一个专用的JS脚本,如果你想将它用在你的web页面上你必须最小程度的使用不同的浏览器测试它并注意它们的版本,许多的脚本程序包含了浏览器类型检测和大量的if...else语句,以处理这样不同。 copyright dedecms
示例页面homepage.htm源代码
<html>
<head>
<title>Homepage</title>
</head>
<body>
<a href='http://www.thehungersite.com/'>Manual redirection<br></a>
<a href="javascript:ResetCookie()">Cookie reset</a>
本文来自织梦
<script language="JavaScript">
<!--
var bVisitedToday = false;
var lastVisit = GetCookie("lastVisit");
if (lastVisit != null)
{
lastVisit = 1 * lastVisit;
var lastHere = new Date(lastVisit);
var rightNow = new Date(); dedecms.com
if(lastHere.getYear() == rightNow.getYear()
&& lastHere.getMonth() == rightNow.getMonth()
&& lastHere.getDate() == rightNow.getDate())
{
bVisitedToday = true;
}
} 织梦内容管理系统
if(bVisitedToday == false)
{
setLastlastVisitCookie();
window.location="http://www.thehungersite.com/"
}
else
{
//window.location="about:blank"
}
织梦内容管理系统
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;
文章评论
共有位Admini5网友发表了评论 查看完整内容