I would like to note that the method name for DOMRect.toJSON
(actually the toJSON
method of DOMRectReadOnly
) is misleading.
It does not return the DOMRect
instance as Javascript String Object Notation (so a string), but a plain Object
.
So, trying to use the DOMRect.toJSON
return value as actual JSON
will throw a SyntaxError
.
const elementDimensions = JSON.parse(
[someElement].getBoundingClientRect().toJSON()
);
// => Uncaught SyntaxError: "[object Object]" is not valid JSON
// One can of course use ...
const elementDimensions = JSON.parse(
JSON.stringify([someElement].getBoundingClientRect().toJSON())
);
// But that would be, well ... refute the meaning of toJSON
I would suggest a line to mention this in the MDN DOMRectReadOnly page
Regards / Kooiinc
[edit] rerouted to MDN topic.