巧用JavaScript记录访问次数(Cookie)!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>刷新页面增加人气</title>
<script type="text/javascript">
<!--
var caution = false;
function set Cookie(name, value, expires, path, domain, secure) {
var curCookie = name + '=' + escape(value) +
((expires) ? '; expires=' + expires.toGMTString() : '') +
((path) ? '; path=' + path : '') +
((domain) ? '; domain=' + domain : '') +
((secure) ? '; secure' : '');
if (!caution || (name + '=' + escape(value)).length <= 4000)
{
document.cookie = curCookie;
}
else
if (confirm('Cookie exceeds 4KB and will be cut!/r/n Cookie超过4KB并且将被cut!')){
document.cookie = curCookie;
}
}
function getCookie(name) {
var prefix = name + '=';
var cookieStartIndex = document.cookie.indexOf(prefix) ;
if (cookieStartIndex == -1)
{
return null;
}
var cookieEndIndex = document.cookie.indexOf(';', cookieStartIndex + prefix.length);
if (cookieEndIndex == -1)
{
cookieEndIndex = document.cookie.length;
}
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + '=' +
((path) ? '; path=' + path : '') +
((domain) ? '; domain=' + domain : '') +
'; expires=Thu, 01-Jan-70 00:00:01 GMT';
}
}
function fixDate(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
{
date.setTime(date.getTime() - skew);
}
}
var now = new Date();
fixDate(now);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
var visits = getCookie('counter');
if (!visits)
{
visits = 1;
}
else
{
visits = parseInt(visits) + 1;
}
setCookie('counter', visits, now);
if(visits < 10)
{
document.write('您是本站的新手,');
}
if(visits >= 10 && visits < 20)
{
document.write('您是本站的初级程序员,');
}
if(visits >= 20 && visits <= 30)
{
document.write('您是本站的中级程序员,');
}
if(visits >= 30)
{
document.write('您是本站的终极支柱啊,我太感动了!');
}
document.writeln('欢迎您的第' + visits + '次光临!');
document.writeln('<br />为了验证程序的正确性,你可以刷新几次后再打开此文件,看看效果!')
// -->
</script>
</head>
<body>
</body>
</html>
这个用 JavaScript访问Cookie制作的保存用户访问次数的小程序,发出来,希望能给大家带来一些好点帮助!
看了要记得回帖……
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>刷新页面增加人气</title>
<script type="text/javascript">
<!--
var caution = false;
function set Cookie(name, value, expires, path, domain, secure) {
var curCookie = name + '=' + escape(value) +
((expires) ? '; expires=' + expires.toGMTString() : '') +
((path) ? '; path=' + path : '') +
((domain) ? '; domain=' + domain : '') +
((secure) ? '; secure' : '');
if (!caution || (name + '=' + escape(value)).length <= 4000)
{
document.cookie = curCookie;
}
else
if (confirm('Cookie exceeds 4KB and will be cut!/r/n Cookie超过4KB并且将被cut!')){
document.cookie = curCookie;
}
}
function getCookie(name) {
var prefix = name + '=';
var cookieStartIndex = document.cookie.indexOf(prefix) ;
if (cookieStartIndex == -1)
{
return null;
}
var cookieEndIndex = document.cookie.indexOf(';', cookieStartIndex + prefix.length);
if (cookieEndIndex == -1)
{
cookieEndIndex = document.cookie.length;
}
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + '=' +
((path) ? '; path=' + path : '') +
((domain) ? '; domain=' + domain : '') +
'; expires=Thu, 01-Jan-70 00:00:01 GMT';
}
}
function fixDate(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
{
date.setTime(date.getTime() - skew);
}
}
var now = new Date();
fixDate(now);
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
var visits = getCookie('counter');
if (!visits)
{
visits = 1;
}
else
{
visits = parseInt(visits) + 1;
}
setCookie('counter', visits, now);
if(visits < 10)
{
document.write('您是本站的新手,');
}
if(visits >= 10 && visits < 20)
{
document.write('您是本站的初级程序员,');
}
if(visits >= 20 && visits <= 30)
{
document.write('您是本站的中级程序员,');
}
if(visits >= 30)
{
document.write('您是本站的终极支柱啊,我太感动了!');
}
document.writeln('欢迎您的第' + visits + '次光临!');
document.writeln('<br />为了验证程序的正确性,你可以刷新几次后再打开此文件,看看效果!')
// -->
</script>
</head>
<body>
</body>
</html>
这个用 JavaScript访问Cookie制作的保存用户访问次数的小程序,发出来,希望能给大家带来一些好点帮助!
看了要记得回帖……