Why not getBoundingClientRect serialization with JSON.toStringify?

Try this in the dev tool console:

JSON.stringify(document.body.getBoundingClientRect()) 

output {}, not something reasonable.

Any ideas?

+5
source share
1 answer

JSON.stringify uses the toJSON method.

You can specify it for your object or, in your case, redefine it:

 ClientRect.prototype.toJSON = function(){ return { top: this.top } } JSON.stringify(document.body.getBoundingClientRect()) "{"top":-583}" 
+2
source

All Articles