window.postMessage does not set event.origin
See original GitHub issueBasic info:
- Node.js version: 10.16.0
- jsdom version: 15.2.1
When calling window.postMessage()
in JSDOM, registered event listeners will receive the event, however the origin
property will be set to an empty string.
post-message.js, specifically calls out event.origin
(and a few others) in a TODO
which I believe (not 100% sure) is why I’m having problems with this
Minimal reproduction case
const { JSDOM } = require("jsdom");
const assert = require('assert');
const dom = new JSDOM(`<!DOCTYPE html>`);
dom.window.addEventListener("message", evt => {
// Expected: evt.origin === "https://sending-domain.com"
// Actual: evt.origin === ""
assert(evt.origin === 'https://sending-domain.com')
})
dom.window.location = "https://sending-domain.com"
dom.window.postMessage('The message being posted', '*')
How does similar code behave in browsers?
In the snippet below, evt.origin
inside the event listener will be equal to window.location
from the frame that called window.postMessage
window.addEventListener("message", evt => { /* handle evt */ })
Issue Analytics
- State:
- Created 4 years ago
- Reactions:15
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Window.postMessage() - Web APIs | MDN
Specifies what the origin of this window must be for the event to be dispatched, either as the literal string "*" (indicating no...
Read more >window.top.postMessage sends "null" as its event.origin
I'm trying to post a message from an iframe to the parent window. I of course want to check the origin of the...
Read more >postMessage(): common issues and how you can mitigate ...
postMessage () method to allow cross-origin communication between different window objects. This method provides a way to circumvent the Same ...
Read more >Using JavaScript and window.postMessage() - In Plain English
Cross-Domain communication (also called Cross-origin) can be ... pop-up window, which we will use, but the recipient window does not have to ...
Read more >8 Communication — HTML 5 - W3C
The following interface is defined for this event: ... If the origin of the target window doesn't match the given origin, the message...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I managed to workaround it. Instead of postMessage i just dispatch a message however would be good to have it properly working
Apparently jsdom also doesn’t set the window event’s source property to the source’s window var. Following the OC test case to reproduce this:
Does it make sense to also create a new bug issue in here?