JSON.stringify(eventObject);
gives:
TypeError: Converting circular structure to JSON
dojox.json.ref.toJson(eventObject);
gives:
TypeError: Accessing selectionEnd on an input element that cannot have a selection.
Is there some library/code ready to use to accomplish it ?
The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
The JSON. stringify method converts a JavaScript object or value to a JSON string. It can optionally modify or filter values if a replacer function/array is specified.
JSON. stringify() takes a JavaScript object and then transforms it into a JSON string. JSON. parse() takes a JSON string and then transforms it into a JavaScript object.
The JSON. stringify() function, as name suggests, converts a JavaScript value to a serialized JSON string. JSON. stringify() can optionally use a replacer function to replace values using custom logic.
You won't be able to serialize an event object with JSON.stringify, because an event object contains references to DOM nodes, and the DOM has circular references all over the place (e.g. child/parent relationships). JSON can't handle these by default, so you're a bit out of luck there.
I'd suggest to look at How to serialize DOM node to JSON even if there are circular references? which has a few suggestions on how to serialize a DOM node. Also, the following questions seem to have useful information:
JSON libraries able to handle circular references seem to be
Alternatively, you could delete all references to DOM nodes if you don't need them, and then serialize the object. You shouldn't do this after all. See @PointedEars comment :)
Use the "replacer" function to avoid errors:
JSON.stringify(evt, function(k, v) { if (v instanceof Node) { return 'Node'; } if (v instanceof Window) { return 'Window'; } return v; }, ' ');
Update 2019: the browser API has changed some way, here is a method to expose all available keys in Event prototype chain
function stringifyEvent(e) { const obj = {}; for (let k in e) { obj[k] = e[k]; } return JSON.stringify(obj, (k, v) => { if (v instanceof Node) return 'Node'; if (v instanceof Window) return 'Window'; return v; }, ' '); }
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