I'm trying to propagate an event from my window.document
to an iframe within this document.
When catching the event in the window.document
I try the following:
event.preventDefault()
(@dispatchTo()).dispatchEvent(event)
# @dispatchTo() returns the reference of `document.querySelector('iframe').contentDocument`
But I get InvalidStateError: Failed to execute 'dispatchEvent' on 'EventTarget': The event is already being dispatched.
I tried preventDefault
and stopPropagation
but none would work. It seems that the event is being dispatched while I try to dispatch it to the iframe document and it fails.
How can I propagate an event to my iframe while catching it from the window.document
?
I do have another eventListener on the iframe for that event, but it doesn't get triggered.
I use React (which has a virtual DOM, it may interfere, it may not, just saying).
I found part-of-a-solution there: https://stackoverflow.com/a/20541207/2391795
And now I'm able to dispatch events from the document to the iframe using this code:
eventClone = new event.constructor(event.type, event)
(@dispatchTo()).dispatchEvent(eventClone)
But since I'm using React, the cloned event isn't equal to the initial event, because React has a kind-of wrapper for events. So I loose many properties, like the which
and isTrusted
, which become false
once cloned.
Is there any way to properly clone a React event?