Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript event dispached in a popup window that has been redirected

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?

like image 559
Paolo Dragone Avatar asked Nov 13 '22 14:11

Paolo Dragone


1 Answers

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.

like image 50
Joe Enzminger Avatar answered Nov 15 '22 07:11

Joe Enzminger