Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stringify DOMWindow object

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.

like image 966
ggutenberg Avatar asked Nov 02 '10 16:11

ggutenberg


2 Answers

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.

like image 140
Alex Avatar answered Sep 30 '22 07:09

Alex


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.

like image 27
Andrew Shooner Avatar answered Sep 30 '22 06:09

Andrew Shooner