Date.now() in IE8

Been tracking down all kinds of fun issues in ie7/8 browsers recently. Here’s the latest gem.

Date.now() 

Date.now() wasn’t added to the JavaScript spec until ECMAScript 5, which means it won’t work in any version of IE < IE9. (See full table here http://kangax.github.com/es5-compat-table/#showold)

This can be especially tricky to catch since it works just fine in IE9 with IE8 compatibility turned on. Yay IE!

For an easy fix, put the following at the top of your JavaScript context.

Date.now = Date.now || function() { return +new Date; }; 

Note: +new Date is shorthand for ToInt32(GetValue(new Date) or (new Date).valueOf() see http://es5.github.com/#x11.4.6 <- this might be the best takeaway

sources/links:

  1. afuchs posted this

Bits and Prattle, powered by Tumblr, Beckett theme by Jonathan Beckett