ie8 Date()兼容性错误

dk1*_*123 8 javascript internet-explorer-8

我在以下javascript的ie8上收到错误:

<script type="text/javascript">
    //when html doc is all ready
    $(document).ready(function () {
        var socket = io.connect();
        var room = 'public';
        socket.emit('join', room);

        socket.on('message', function (data) {
            var output = '';
            output += '<div class="trace-content">';
            output += ' <div class="mname">' + data.name + '</div>';
            output += ' <div class="mdate">' + data.date + '</div>';
            output += ' <p class="mtext">' + data.message + '</p>';
            output += '</div>';

            $(output).prependTo('#traces');
        });

        $('button').click(function () {
            var date = new Date().toISOString();
           socket.emit('message', {
               name: $('#name').val(),
               message: $('#message').val(),
               date: date.slice(2,10) + ' ' + date.slice(11, 19)
           });
        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)

问题似乎在于:var date = new Date().toISOString(); 我无法确定问题的确切位置.其他一切似乎都很好; 只需按下该按钮,然后执行代码.有任何想法吗?

Bil*_*ill 24

IE8不支持.toISOString().您可以将此代码用作垫片(来自Mozilla):

if ( !Date.prototype.toISOString ) {         
    (function() {         
        function pad(number) {
            var r = String(number);
            if ( r.length === 1 ) {
                r = '0' + r;
            }
            return r;
        }      
        Date.prototype.toISOString = function() {
            return this.getUTCFullYear()
                + '-' + pad( this.getUTCMonth() + 1 )
                + '-' + pad( this.getUTCDate() )
                + 'T' + pad( this.getUTCHours() )
                + ':' + pad( this.getUTCMinutes() )
                + ':' + pad( this.getUTCSeconds() )
                + '.' + String( (this.getUTCMilliseconds()/1000).toFixed(3) ).slice( 2, 5 )
                + 'Z';
        };       
    }() );
}
Run Code Online (Sandbox Code Playgroud)