I want to save locally an object which has circular references. What are my options?
My first thought was using HTML5 local storage but I can't stringify this object due to the circular references.
Specifically I'm trying to save the DOMSelection object of the current selection.
Example:
var sel = window.getSelection();
var selstring = JSON.stringify(sel); // Breaks here ...
localStorage.setItem("selection",selstring);
The only way I could get the stringify to work now is by ignoring certain objects like so:
var selstring = JSON.stringify(sel,function(k,v){
if( k=="anchorNode" ||
k=="baseNode" ||
k=="extentNode" ||
k=="focusNode") return undefined;
return v;
});
But this leaves me with a rather empty DOMSelection object which isn't enough for what I need.
Is there any other way I can save this object? The only requirement is that it runs in mobile safari, anything else goes really. The solution can be either in javascript or jquery (or any other js lib if need be).
Thanks for any help you can provide.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…