IE JS类型错误:对象不支持此属性或方法

时间:2012-10-09 21:59:25

标签: javascript internet-explorer internet-explorer-8

我正在尝试编写一个Cookie来跟踪访问者的新近度,并且我正在使用IE8进行攻击。我的日志为IE用户显示的错误是不太有用的: TypeError:对象不支持此属性或方法。

知道我需要在下面的代码中进行修改,以便与IE一起使用吗?

var currentDate = Date.now();
var cookies = document.cookie.match(/jcom\=.+\}/);

if (cookies == null) {
var cookieBase = '{"created":"createdNull","lastVisit":"visitNull"}';
var siteTime = $.parseJSON(cookieBase);
siteTime.created = currentDate;
siteTime.lastVisit = currentDate;
cookieOut = "{\"created\":" + siteTime.created + ",\"lastVisit\":" + siteTime.lastVisit + "};expires=Thu, 2 Aug 2020 20:47:11 UTC;path=/";
document.cookie="jcom=" + cookieOut;
}
else {
var cookies = document.cookie.match(/jcom\=.+\}/)[0];
var cookieSplit1 = cookies.split('=')[1];
var cookieSplit= cookieSplit1.split(';')[0]
var siteTime = $.parseJSON(cookieSplit);
siteTime.lastVisit = currentDate;
cookieOut = "{\"created\":" + siteTime.created + ",\"lastVisit\":"+ siteTime.lastVisit + "};expires=Thu, 2 Aug 2020 20:47:11 UTC;path=/";
document.cookie="jcom=" + cookieOut;
}

谢谢!

1 个答案:

答案 0 :(得分:5)

Internet Explorer 8不支持Date.now()。你可以很容易地自己实现它:

if( !Date.now) {
    Date.now = function() {return new Date.getTime();};
}