ie7 evalJSON无效

时间:2015-02-02 10:02:27

标签: javascript json wordpress internet-explorer-7 prototypejs

我正在尝试将最近的页面小部件放在wordpress上。我自己创造了js。在IE7上它总是打破。我收录了json2json3prototype.js

我的代码在

之下
var newRecentlyViewed = function(opts) {
    var that = {};
    var opts = opts || {};
    var cookieName = opts.cookieName || 'recently-viewed';
    var popupId = opts.popupId || 'recently-viewed-popup';
    var maxRecent = opts.maxRecent || 5;
    var cookieLifeHours = opts.cookieLifeHours || (24 * 0 * 0); /* 24 hours * 30 days * 24 months = 2 years */

    var futureDate = function(nHours) {
        var expire = null;
        if (nHours != null && nHours != 0) {
            var today = new Date();
            expire = new Date();
            expire.setTime(today.getTime() + 3600000 * nHours);
        }
        return expire;
    };

    var setCookie = function(name, value, path, domain, secure, nHours) {
        var expires = futureDate(nHours);
        var curCookie = name + "=" + escape(value)
                + ((path) ? "; path=" + path : "")
                + ((domain) ? "; domain=" + domain : "")
                + ((expires) ? "; expires=" + expires : "")
                + ((secure) ? "; secure" : "");
        document.cookie = curCookie;
    };

    var getCookie = function(name) {
        var dc = document.cookie;
        var prefix = name + "=";
        var begin = dc.indexOf("; " + prefix);
        if (begin == -1) {
            begin = dc.indexOf(prefix);
            if (begin != 0)
                return null;
        } else {
            begin += 2;
        }
        var end = document.cookie.indexOf(";", begin);
        if (end == -1) {
            end = dc.length;
        }
        return unescape(dc.substring(begin + prefix.length, end));
    };

    var recent = function() {
        var cookie = getCookie(cookieName);
        var recentlyViewed = [];
        if (cookie != null) {
            recentlyViewed = cookie.evalJSON();
        }
        return recentlyViewed;
    };

    var remember = function(recentObj, uniqueKey) {
        var recently = recent();
        var now = new Date();
        now = now.getTime();
        var i;
        var dup_idx = -1;
        for (i = 0; i < recently.length; i += 1) {
            if (recently[i][uniqueKey] == recentObj[uniqueKey])
                dup_idx = i;
        }
        if (dup_idx > -1)
            recently.splice(dup_idx, 1);
        recently.push(recentObj);
        if (recently.length > maxRecent) {
            recently.splice(0, recently.length - maxRecent);
        }
        setCookie(cookieName, JSON.stringify(recently), "/", document.domain, false,
                cookieLifeHours);
    };

    /* Public functions are assigned to that */

    that.rememberPage = function(obj) {
        if (obj == null) {
            obj = {
                url : '' + document.location,
                title : document.title
            };
        }
        remember(obj, 'url');
    };

    that.toggleDisplayPopup = function() {
        if ($(popupId).style.display != 'inline') {
            $(popupId).style.display = 'inline';
        } else {
            $(popupId).style.display = 'none';
        }
    };

    that.renderPopup = function() {
        var recentlyViewed = recent();
        var s, i;
        var url = top.location.href;
        if (url.search(/multi-client-data-library/i) < 1) {
            var recentlyViewedItemsContainer = $$("#" + popupId + " .recently-viewed-items")[0];
            if(jQuery(recentlyViewedItemsContainer).length){
                var recentlyViewedItems = recentlyViewedItemsContainer.select(".item");
                /* remove any existing div.item */
                for (i = 0; i < recentlyViewedItems.length; i++) {
                    recentlyViewedItems[i].remove();
                }
                for (i = 0; i < recentlyViewed.length; i++) {               
                    var recently = recentlyViewed[i];
                    var title = recently.title.substring(0, recently.title.length - 15);
                    if(title.length > 30) title = title.substring(0,27) + "... ";
                    s = "<div class=\"item\"><a href=\"" + recently.url + "\">"
                            + title + "</a></div>";
                    recentlyViewedItemsContainer.insert({
                        top : s
                    });
                }
            }
        }
    };

    that.clear = function() {
        setCookie(cookieName, JSON.stringify([]), "/", document.domain, false,
                cookieLifeHours);
        that.renderPopup();
    };

    that.onload = function() {
        that.rememberPage(); /* accessing a public function from a public function require that.*/
        that.renderPopup();
    };

    return that;
};

我的current网站似乎运行正常。如果您浏览IE7右侧最近页面widgt的某些页面,您将看到没有错误。如果您浏览到developing网站,则无效。

ie7 screenshot

我已恢复为prototype.js v1.6.1。 JSON不再抱怨了,但是我的代码中断了没有意义,因为实时版本没有任何改变。我也在prototype.js

中收到错误消息
Object doesn't support property or method 'getElementsByClassName'

0 个答案:

没有答案
相关问题