2012-10-16 48 views
8

我在IE8得到一个错误以下的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> 

的问题似乎是在该行:VAR日期=新的日期()toISOString(); 我遇到了麻烦,指出究竟是什么问题。 其他一切似乎工作正常;只需点击该按钮并通过下面的代码。有任何想法吗?

回答

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'; 
     };  
    }()); 
} 
+0

很简单的一段代码。谢谢! – dk123

3

当然,你得到一个错误http://kangax.github.com/es5-compat-table。在Google中查找date.prototype.toISOString()填充。发现这个https://gist.github.com/1044533

从要点:

// thanks to @fgnass and @subzey for their awesome golf skills 
// annotation by @fgnass 

function(a){ 
    a=this; 
    return (
    1e3 // Insert a leading zero as padding for months < 10 
    -~a.getUTCMonth() // Months start at 0, so increment it by one 
    *10 // Insert a trailing zero as padding for days < 10 
    +a.toUTCString() // Can be "1 Jan 1970 00:00:00 GMT" or "Thu, 01 Jan 1970 00:00:00 GMT" 
    +1e3+a/1 // Append the millis, add 1000 to handle timestamps <= 999 
    // The resulting String for new Date(0) will be: 
    // "-1010 Thu, 01 Jan 1970 00:00:00 GMT1000" or 
    // "-10101 Jan 1970 00:00:00 GMT1000" (IE) 
    ).replace(
     // The two digits after the leading '-1' contain the month 
     // The next two digits (at whatever location) contain the day 
     // The last three chars are the milliseconds 
     /1(..).*?(\d\d)\D+(\d+).(\S+).*(...)/, 
    '$3-$1-$2T$4.$5Z') 
} 

注:这可能不是最可读的代码或填充工具的最好的例子,但它似乎根据要点的意见所以这是一个工作快速复制/粘贴解决方案。

+0

哇,我不想维护那个代码:)(高尔夫代码是搞清楚如何用最少的字符完成某件事)。 – Bill

+0

阅读评论是一个140字节的例子。我的观点是向OP展示他应该在寻找什么。这意味着一个复制/粘贴解决方案。评论似乎暗示有更好的选择,但人们报告它在IE6和其他浏览器中工作。 – elclanrs

+0

感谢您的详细解答。我一定会查看你已经链接的表格。我选择接受比尔的答案,尽管纯粹是因为我将无法记住这种复杂性的代码在未来的时间点会做什么;尽管很大的帮助,谢谢! – dk123

0

为什么不使用toJSON method代替? 它受IE8支持。

+0

这不提供问题的答案。要批评或要求作者澄清,请在其帖子下方留言。 - [来自评论](/ review/low-quality-posts/10597816) –

+0

@JonSurrell你尝试过使用这种方法吗?它确实解决了他的问题,因为[调用'toJSON'返回一个代表Date对象值的字符串](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON#Description )。 – Knu