【解决】js网页倒计时碰到的问题

我在做网页倒计时的时候遇到一些js兼容的问题,这里和大家说一下

首先我在网络上找了两三个js都能很好的实现倒计时效果,

其中下面这个:

[code lang=”js”]
<SCRIPT>
function setcountdown(theyear,themonth,theday){
yr=theyear;mo=themonth;da=theday
}
setcountdown(2012,2,28)
var occasion=""
var message_on_occasion="shijian dao le"
var countdownwidth=”
var countdownheight=”
var countdownbgcolor=”
var opentags='<font face="Arial"><small>’
var closetags='</small></font>’
var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
var crosscount=”
function start_countdown(){
if (document.layers)
document.countdownnsmain.visibility="show"
else if (document.all||document.getElementById)
crosscount=document.getElementById&&!document.all?document.getElementById("countdownie") : countdownie
countdown()
}
if (document.all||document.getElementById)
document.write(‘<span id="countdownie" style="width:’+countdownwidth+’; background-color:’+countdownbgcolor+’"></span>’)
window.onload=start_countdown
function countdown(){
var today=new Date()
var todayy=today.getYear()
if (todayy < 1000)
todayy+=1900
var todaym=today.getMonth()
var todayd=today.getDate()
var todayh=today.getHours()
var todaymin=today.getMinutes()
var todaysec=today.getSeconds()
var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysec
futurestring=montharray[mo-1]+" "+da+", "+yr
dd=Date.parse(futurestring)-Date.parse(todaystring)
dday=Math.floor(dd/(60*60*1000*24)*1)
dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)
dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)
dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)
if(dday<=0&&dhour<=0&&dmin<=0&&dsec<=1&&todayd==da){
if (document.layers){
document.countdownnsmain.document.countdownnssub.document.write(opentags+message_on_occasion+closetags)
document.countdownnsmain.document.countdownnssub.document.close()
}
else if (document.all||document.getElementById)
crosscount.innerHTML=opentags+message_on_occasion+closetags
return
}
else if (dday<=-1){
if (document.layers){
document.countdownnsmain.document.countdownnssub.document.write(opentags+"Alpha test!"+closetags)
document.countdownnsmain.document.countdownnssub.document.close()
}
else if (document.all||document.getElementById)
crosscount.innerHTML=opentags+"Occasion already passed! "+closetags
return
}
else{
if (document.layers){
document.countdownnsmain.document.countdownnssub.document.write(opentags+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds left until "+occasion+closetags)
document.countdownnsmain.document.countdownnssub.document.close()
}
else if (document.all||document.getElementById)
crosscount.innerHTML=opentags+"还有"+dday+ "天"+dhour+"小时 "+dmin+"分钟 "+dsec+"秒 "+occasion+closetags
}
setTimeout("countdown()",1000)
}
</SCRIPT>
[/code]

不需要任何添加,就这些代码就可以实现输出倒计时效果

下面还有两个:
1.
[code]
<SCRIPT LANGUAGE="JavaScript">
<!– Begin
function getTime() {
now = new Date();
y2k = new Date("Feb 28 2012 14:00:00");
days = (y2k – now) / 1000 / 60 / 60 / 24;
daysRound = Math.floor(days);
hours = (y2k – now) / 1000 / 60 / 60 – (24 * daysRound);
hoursRound = Math.floor(hours);
minutes = (y2k – now) / 1000 /60 – (24 * 60 * daysRound) – (60 * hoursRound);
minutesRound = Math.floor(minutes);
seconds = (y2k – now) / 1000 – (24 * 60 * 60 * daysRound) – (60 * 60 * hoursRound) – (60 * minutesRound);
secondsRound = Math.round(seconds);
sec = (secondsRound == 1) ? " second." : " seconds.";
min = (minutesRound == 1) ? " minute" : " minutes, ";
hr = (hoursRound == 1) ? " hour" : " hours, ";
dy = (daysRound == 1) ? " day" : " "
document.timeForm.input1.value = "剩余时间: " + daysRound + dy + hoursRound + hr + minutesRound + min + secondsRound + sec;
newtime = window.setTimeout("getTime();", 1000);
}
// End –>
</script>
<body bgcolor="#fef4d9" onLoad="getTime()">

<form name=timeForm>
<input type=text name=input1 size=70 border-style="none" style="border-bottom: 0px solid; border-left: 0px solid;border-right: 0px solid;border-top: 0px solid;font:12px arial, helvetica,sans-serif">
</form>
</body>
[/code]
我用的是下面这个,很好的兼容效果:
[code]
<div class="daojishi png"><SPAN style="position:absolute; right:3px; cursor:pointer" >X</SPAN>

<div style="position:absolute; left:50px; bottom:50px; ">
<script language="JavaScript">
<!–
var DifferenceHour = -1
var DifferenceMinute = -1
var DifferenceSecond = -1
var Tday = new Date("Feb 27, 2012 23:59:59")
var daysms = 24 * 60 * 60 * 1000
var hoursms = 60 * 60 * 1000
var Secondms = 60 * 1000
var microsecond = 1000
function clock()
{
var time = new Date()
var hour = time.getHours()
var minute = time.getMinutes()
var second = time.getSeconds()
var timevalue = ""+((hour > 12) ? hour-12:hour)
timevalue +=((minute < 10) ? ":0":":")+minute
timevalue +=((second < 10) ? ":0":":")+second
timevalue +=((hour >12 ) ? " PM":" AM")
// document.formnow.now.value = timevalue
var convertHour = DifferenceHour
var convertMinute = DifferenceMinute
var convertSecond = DifferenceSecond
var Diffms = Tday.getTime() – time.getTime()
DifferenceHour = Math.floor(Diffms / daysms)
Diffms -= DifferenceHour * daysms
DifferenceMinute = Math.floor(Diffms / hoursms)
Diffms -= DifferenceMinute * hoursms
DifferenceSecond = Math.floor(Diffms / Secondms)
Diffms -= DifferenceSecond * Secondms
var dSecs = Math.floor(Diffms / microsecond)
if(convertHour != DifferenceHour) document.formnow.dd.value=DifferenceHour
if(convertMinute != DifferenceMinute) document.formnow.hh.value=DifferenceMinute
if(convertSecond != DifferenceSecond) document.formnow.mm.value=DifferenceSecond
document.formnow.ss.value=dSecs
// document.formnow.Tnow.value= DifferenceHour DifferenceMinute + DifferenceSecond + dSecs
setTimeout("clock()",1000)
}
// end hiding –>
</script><form name="formnow">
<input type="text" name="dd" style="width:40px; background:none; border:none;color:#ffa200; font-size:24px; font-weight:bold">
<input type="text" name="hh" style="width:50px; background:none; border:none;color:#ffa200; font-size:24px; font-weight:bold">
<input type="text" name="mm" style="width:40px; background:none; border:none;color:#ffa200; font-size:24px; font-weight:bold">
<input type="text" name="ss" style="width:30px; background:none; border:none;color:#ffa200; font-size:24px; font-weight:bold">
</form>
</div>

<div style="position:absolute; right:50px; bottom:45px; color:#ffffff; font-size:18px ">
Days&nbsp;&nbsp;&nbsp;&nbsp;Hours&nbsp;&nbsp;&nbsp;&nbsp; Min&nbsp;&nbsp;&nbsp;&nbsp; Sec
</div>
</div>
[/code]
用onLoad=”clock(),initurl()”调用它,如果有两个onLoad用逗号隔开他!。就这样了。