2

我制作了一个 Web 应用程序,当用户单击某个 nenu 时,它会在新窗口中打开,并且如果用户已经在某个窗口中打开了该菜单以专注于它。这是任务的简单部分。我为任何菜单选项创建了一个带有一些键的 window.open 对象数组。

function menuWindows(index, URL){
    if (openedWindows.length < index){
        openedWindows.length = index;
    }
    if ((openedWindows[index] == undefined) || openedWindows[index].closed){
        openedWindows[index]= window.open(URL, index);
        setOpenedWindows();
    } else {
        openedWindows[index].focus();
    }
}

我的想法是将这个数组存储在 cookie 中,并且在检查之前已经有一个打开的窗口可以从 cookie 中加载一个数组。问题是当我尝试将该数组存储在 cookie 中时。我在 cookie 中存储数组 opensWindows 的函数的第一个版本是:

function setOpenedWindows(){
    $.cookie('openedWindows', JSON.stringify(openedWindows));
}

在浏览器控制台中的错误是:“将循环结构转换为 JSON”。经过镜头搜索后,我找到了这篇文章,我的第二个版本的商店功能是:

function setOpenedWindows(){
    var cache = [];
    var ow = JSON.stringify(openedWindows, function(key, value) {
        if (typeof value === 'object' && value !== null) {
            if (cache.indexOf(value) !== -1) {
                // Circular reference found, discard key
                return;
            }
            // Store value in our collection
            cache.push(value);
        }
        return value;
    });
    cache = null; // Enable garbage collection
    $.cookie("openedWindows", ow);
}

现在我有这个错误:“未捕获的 RangeError:超出了最大调用堆栈大小”,我不知道该怎么办。

4

0 回答 0