IE8 不支持Date.now()

今天利用ueditor插件时,要产生两个id,原本使用

 1  link:function(scope, element, attrs, ctrl){
 2       var id = attrs.id  || 'ueditor_' + Date.now(); //id in directive, or random
 3       var width = attrs.width || '100%'; //number without unit 'px' or percentage number in attrs, default 100%
 4       var height = attrs.height || '150'; //number without unit 'px' or percentage number in sttrs, default 100%
 5       var autoHeight = !!attrs.autoHeight || false; //Default false
 6 
 7       element[0].id = id;
 8 
 9       var ue= UE.getEditor(id, { 
10         initialFrameWidth: '100%',
11         initialFrameHeight: height,
12         autoHeightEnabled: autoHeight
13       });
14 ...

 

Date.now()

返回1970 年 1 月 1日午夜与当前日期和时间之间的毫秒数。

 

以下是微软的描述:

在早于 Internet Explorer 9 的安装版本中不受支持。 但是,在以下文档模式中受到支持:Quirks、Internet Explorer 6 标准、Internet Explorer 7 标准、Internet Explorer 8 标准、Internet Explorer 9 标准和 Internet Explorer 10 标准。 在 Windows 应用商店 应用程序中也受支持。

https://msdn.microsoft.com/library/ff679974.aspx 

但是经测试在IE8,标准模式下并未支持!所以需采用其他方法:

getTime 方法返回 1970 年 1 月 1日与指定日期之间的毫秒数。

使用方法:dateObj.getTime()   

返回 1970 年 1 月 1 日午夜与 Date 对象中的时间值之间的毫秒数。日期范围在 1970 年 1 月 1 日的午夜前后都有大约 285,616 年。负数指示 1970 年之前的日期。

 var id = attrs.id  || 'ueditor_' + new Date().getTime(); //id in directive, or random

 

 

例子:

 1 var minute = 1000 * 60;
 2 var hour = minute * 60;
 3 var day = hour * 24;
 4 
 5 date = new Date("1/1/2001");
 6 var time = date.getTime();
 7 
 8 document.write(Math.round(time / day) + " days from 1/1/1970 to 1/1/2001");
 9 
10 // Output: 11323 days from 1/1/1970 to 1/1/2001

 

参考:https://msdn.microsoft.com/zh-cn/library/7hcawkw2.aspx

 

posted @ 2016-07-11 10:20  shiddong  阅读(1520)  评论(0编辑  收藏  举报