人们都希望自己的主页生动活泼,并且多一些与访问者的交互。下面的小技巧是否能让你感到自己的网页多了几分色彩呢? 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) 看到这么亲切的问候词,你的访问者一定会多一份对你网页的关心和呵护。