For some reason I seem unable to use JSON.stringify on a DOMWindow object. For example:
console.log(window.self); // Outputs a hierarchical DOMWindow object
console.log(JSON.stringify(window.self)); // Outputs nothing - not even an error
alert(window.self); // Alerts "[object DOMWindow]"
alert(JSON.stringify(window.self)); // Again nothing - not even an error
Tested on Safari and Chrome. Does anyone have any ideas how I can achieve this?
Edit:
Moved edit to a new question as it's not really specific to this.
Why would you want to do serialize the DOM? If you must, Crescent's link is where you need to look. The reason you cannot serialize (stringify) the window object is because it contains circular references, and JSON.stringify
does not support them by default.
As others have said, stringify doesn't support circular references, which DOMWindow
contains. Generally, circular references could be converted to JSON using Douglas Cockford's JSON cycle.js.
However, I just tried this on window
, and it causes a stack overflow anyway. While that may be a bug in the cycle.js code, it seems like its more likely that window
is just too big of an object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With