I have a page A that opens a popup to B. Page B, after some jobs, always redirects to another page C in the pop up. Page C then dispatches an event to send some data to page A, but page A has no reference to page C to register the event handler. The code I tried is somewhat like this:
PageA:
function handler(e) {
alert(e.detail.message);
}
var popup = window.open('/PageB.aspx');
popup.addEventListener("dispatch", handler, false);
PageB:
location.href = "PageC.aspx";
PageC:
var event = new CustomEvent(
"dispatch",
{
detail: {
message: "Test"
},
bubbles: true,
cancelable: true
}
);
window.dispatchEvent(event);
But this is not working, because when I redirect, PageA misses the reference to PageC. Does anyone knows a way to fix this?
If Page C is in the same domain as page A, try
window.opener.dispatchEvent(event);
If not, you need to use postMessage instead, which means you have to serialize your event to a string and listen for messages on Page A.
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