I make a web application that when user click on some nenu it's open in new window, and if user has already opened that menu in some window to focus on it. This is easy part of task. I make a array of window.open object with some key for any of menu option.
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();
}
}
My idea is to store this array in cookies and before check allready have a open window to load a array from cookies. The problem is when I try to store that array in cookie. A first version of my function that store array openedWindows in cookie was:
function setOpenedWindows(){
$.cookie('openedWindows', JSON.stringify(openedWindows));
}
In browser console error was: "Converting circular structure to JSON". After a shot search I find this post and my second version of store function is:
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);
}
Now I have this error: "Uncaught RangeError: Maximum call stack size exceeded" and I don't know what to do.