0

以下 JavaScript 在 Firefox 中不起作用,我能得到的唯一调试错误是“[06:29:53.518] SyntaxError: 09 is not a legal ECMA-262 octal constant”,它与 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 onload="javascript:init();">
4

2 回答 2

0

date('m', $time)在 php 中返回字符串“09”。当你将它插入到你的 javascript 中而不带引号时,你会得到类似var month = 09. Javascript 将带有前缀 0 的数字视为八进制数,并且作为八进制数仅在数字 0 到 7 之后,没有八进制数 9。您可以intval()在 php ( docs ) 中使用将字符串转换为整数:intval( date('m', $time), 10 );在插入之前它进入你的JavaScript。

编辑:更明智的方法可能是使用不包含前缀 0 ( date( 'n', $time );) 的月份选项或在其周围添加引号以使其成为字符串。一个月中的某天(j而不是d)、小时、分钟和秒也是如此。请参阅文档

于 2013-09-15T05:45:33.447 回答
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);
}
于 2013-09-15T09:13:07.727 回答