技巧|網頁 人們都希望自己的首頁生動活潑,並且多一些與訪問者的互動。下面的小技巧是否能讓你感到自己的網頁多了幾分色彩呢?
1.來訪者訪問次數的記錄
一位來訪者看到我能統計他訪問我的首頁的次數時,非常驚訝,他在留言簿裡問我是不是發了駭客程式給他,其實就是下面這段小程式(見程式1)。
<script
LANGUAGE="JavaScript">
<!--
var caution = false
function setCookie(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!"))
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)
document.write("我想您是第"+visits+"次來這兒了!很高興又見到你噢^_^!")
// -->
</script>
(程式1)
2.可以根據時間變化的歡迎詞
下面這段小程式能夠根據訪問者來訪的時間的不同而致以不同的歡迎詞(見程式2)。
<script
language="Javascript">
<!-- Hiding
var now = new Date()
var year=now.getYear()
var month=now.getMonth()+1;
var date= now.getDate()
var time = now.getHours()
if (time>=0)
{var hello="你真是個夜貓子!這麼晚了還在閑逛,你知道嗎?"}
if (time>=6)
{var hello="現在還是挺早的,"}
if (time>=9)
{var hello="小心老闆哦,"}
if (time>=12)
{var hello="中午好呀!"}
if (time>13)
{var hello="中午睡覺了嗎?"}
if (time>=18)
{var hello="吃晚飯了嗎?"}
if time>19)
{var hello="怎麼沒看電視?"}
if (time>23)
{var hello="已經很晚了,還不去睡覺?你知道不?"}
var hour1=(now.getHours() >=10)?"":"0"
var m2=(now.getMinutes()>=10)?"":"0"
var t=(now.getHours()>12)?now.getHours()-12:now.getHours()
var r2 = (t >= 10) ? "" : ""
var color=(now.getSeconds()%2==0)?"ffff80":"00000f"
dcument.write("<font color=\"#",color,"\">",hello,"現在是北京時間:19",year,"年",month,"月",date,"日,",hour1,now.getHours(),"點",m2,now.getMinutes(),"分",((now.getSeconds()<10)?"0":"")+now.getSeconds(),"秒了","</font>")
timerRunning=true
//end hiding contents -->
</script>
(程式2)
看到這麼親切的問候詞,你的訪問者一定會多一份對你網頁的關心和呵護。