[英]ie8 Date() compatibility error
I'm getting an error on ie8 for the following javascript: 我在以下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>
The problem seems to be in the line: var date = new Date().toISOString(); 问题似乎在于:var date = new Date()。toISOString(); I'm having trouble pin pointing what exactly the problem is. 我无法确定问题的确切位置。 Everything else seems to working fine; 其他一切似乎都很好; just that button click and the code following through. 只需按下该按钮,然后执行代码。 Any ideas? 有任何想法吗?
IE8 doesn't support .toISOString()
. IE8不支持.toISOString()
。 You can use this code as a shim (from Mozilla ) : 您可以将此代码用作垫片(来自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';
};
}() );
}
Of course you get an error http://kangax.github.com/es5-compat-table . 当然你得到一个错误http://kangax.github.com/es5-compat-table 。 Look for date.prototype.toISOString() polyfill in Google. 在Google中查找date.prototype.toISOString()polyfill 。 Found this https://gist.github.com/1044533 找到了这个https://gist.github.com/1044533
From the gist: 从要点:
// 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')
}
Note: This might not be the most readable code or best example of polyfill but it seems to work according to the comments in the gist so it's a quick copy/paste solution. 注意:这可能不是最易读的代码或polyfill的最佳示例,但它似乎根据要点中的注释工作,因此它是一个快速复制/粘贴解决方案。
Why aren't you using the toJSON method instead? 为什么不使用toJSON方法呢? It is supported by IE8. 它受IE8支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.