6

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Paolo Dragone
  • 939
  • 1
  • 11
  • 27

1 Answers1

1

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.

Joe Enzminger
  • 11,110
  • 3
  • 50
  • 75
  • Page C is in the same domain, but in the stuff done by Page B there is also a redirect to another domain that turn to Page C, so the reference to opener is lost. I think that I must use postMessage, but Page A still not have a refecerence to Page C so, can you show me how to do this? – Paolo Dragone Dec 19 '12 at 15:05
  • postMessage won't work unless you have a reference to the parent window. Try window.parent instead. – Joe Enzminger Dec 19 '12 at 15:45
  • This [related question](http://stackoverflow.com/questions/7120534/window-opener-is-null-after-redirect) might help. – Joe Enzminger Dec 20 '12 at 01:14