以下JavaScript在Firefox中不起作用,我能得到的唯一调试错误是“[06:29:53.518] SyntaxError:09不是合法的ECMA-262八进制常量”,它与var month ='有关。 date('m',$ time)。';线。
此代码在Internet Explorer和Chrome中运行良好,只有在Firefox中它才能正常工作,尽管使用警告框突出显示它通常会停止的行,但不会提示该问题。
var hours = '.date('G', $time).';
var minutes = '.date('i', $time).';
var seconds = '.date('s', $time).';
var day = '.date('d', $time).';
var month = '.date('m', $time).';
var year = '.date('o', $time).';
function getDayCount(monthIndex, yearIndex)
{
switch ( monthIndex )
{
case 0: return 31;
case 1: return isLeapYear(yearIndex) ? 29 : 28;
case 2: return 31;
case 3: return 30;
case 4: return 31;
case 5: return 30;
case 6: return 31;
case 7: return 31;
case 8: return 30;
case 9: return 31;
case 10: return 30;
case 11: return 31;
}
return 31;
}
function isLeapYear(yearIndex)
{
return new Date(yearIndex, 2-1, 29).getDate() == 29;
}
function getDayName(year, month, day, hours, minutes, seconds)
{
var d = new Date(year, month, day, hours, minutes, seconds, 0);
var weekday= new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
return weekday[d.getDay()];
}
function updateClock()
{
window.seconds++;
if ( window.seconds == 60 ) {
window.minutes++;
window.seconds = 0;
}
if ( window.minutes == 60 ) {
window.hours++;
window.minutes = 0;
}
if ( window.hours == 24 ) {
window.day++;
window.hours = 0;
}
if ( (window.day - 1) == getDayCount(window.month - 1, window.year) ) {
window.month++;
window.day = 1;
}
if ( window.month == 12 ) {
window.year++;
window.month = 1;
}
var hourString = window.hours.toString();
var minuteString = window.minutes.toString();
var secondsString = window.seconds.toString();
var dayString = window.day.toString();
var monthString = window.month.toString();
var yearString = window.year.toString();
if ( minuteString.length < 2 )
minuteString = "0".concat(minuteString);
if ( hourString.length < 2 )
hourString = "0".concat(hourString);
if ( secondsString.length < 2 )
secondsString = "0".concat(secondsString);
if ( dayString.length < 2 )
dayString = "0".concat(dayString);
if ( monthString.length < 2 )
monthString = "0".concat(monthString);
document.getElementById("hour").innerText = hourString;
document.getElementById("minute").innerText = minuteString;
document.getElementById("second").innerText = secondsString;
document.getElementById("day").innerText = dayString;
document.getElementById("month").innerText = monthString;
document.getElementById("year").innerText = yearString;
document.getElementById("dayName").innerText = getDayName(year, month - 1, day, hours, minutes, seconds);
}
function init()
{
window.setInterval(function(){updateClock()}, 1000);
}
关注的HTML:
<h2>Live Chat - <span id="hour"></span>:<span id="minute"></span>:<span id="second"></span> <span id="dayName"></span> <span id="day"></span>/<span id="month"></span>/<span id="year"></span></h2>
Body init:
<body onload="javascript:init();">
答案 0 :(得分:0)
date('m', $time)
返回字符串“09”。当您将其插入到没有引号的javascript中时,您会得到var month = 09
之类的内容。 Javascript将前缀为0的数字视为八进制数,并且将八进制数字视为未来数字0到7,没有八进制数9.您可以在php(docs)中使用intval()
来在将字符串插入javascript之前,将字符串转换为整数:intval( date('m', $time), 10 );
。
编辑:一种更明智的方法可能是使用不包含前缀0(date( 'n', $time );
)的月份选项或在其周围添加引号以使其成为字符串。对于月中的某一天(j
而不是d
),小时,分钟和秒,情况也是如此。请参阅docs。
答案 1 :(得分:0)
解决方案是使用textContent而不是innerText,因为firefox不支持innerText
http://blog.coderlab.us/2005/09/22/using-the-innertext-property-with-firefox/
if(document.all) {
document.getElementById("hour").innerText = hourString;
document.getElementById("minute").innerText = minuteString;
document.getElementById("second").innerText = secondsString;
document.getElementById("day").innerText = dayString;
document.getElementById("month").innerText = monthString;
document.getElementById("year").innerText = yearString;
document.getElementById("dayName").innerText = getDayName(year, month - 1, day, hours, minutes, seconds);
}
else {
document.getElementById("hour").textContent = hourString;
document.getElementById("minute").textContent = minuteString;
document.getElementById("second").textContent = secondsString;
document.getElementById("day").textContent = dayString;
document.getElementById("month").textContent = monthString;
document.getElementById("year").textContent = yearString;
document.getElementById("dayName").textContent = getDayName(year, month - 1, day, hours, minutes, seconds);
}