views:

756

answers:

2

Hello SO,

I've got a situation a page on where a script on www.example.com/index.html opens home.example.com/foo.html in a popup window. When the user closes the popup, I want to notify the opener page by calling a Javascript function on it (which does a few things with the DOM). I use unbeforeunload like this:

    // In index.html on www.example.com:
    window.fn = function () { /* Perform stuff after foo.html has closed */ }

    // In foo.html on home.example.com:
    window.onbeforeunload = function () {
        if (window.opener && window.opener.fn)
            window.opener.fn();
    };

This doesn't work because the web pages are on different domains. I can set the document.domain property to overcome this:

document.domain = "example.com";

Unfortunately, this doesn't play well with the web app framework I use on the foo.html side (Apache Wicket), as it includes a script which does something like this:

var src = (window.location.protocol == 'https:') ? something : other;

Apparently, in IE6*, when you set the document domain, the location object becomes write-only, and so trying to read window.location.protocol throws "Access denied".

So, my question is: How do I allow cross-domain Javascript function calls while still allowing my scripts to read the contents of the location object?

  • I can't go via the server. (The work performed by the function I want to call doesn't really play that way.)
  • I can't read the window.location.protocol property before setting document.domain and then use that value in the conditional assignment; doing so would require me to rebuild the web framework libraries - not something I want to do.


* Possibly in other versions of IE, too; haven't checked.

A: 

I can't read the window.location.protocol property before setting document.domain and then use that value in the conditional assignment; doing so would require me to rebuild the web framework libraries - not something I want to do.

can't you read window.location.protocol prior to to setting document.domain and set window.location.protocol once it becomes write-only? Would that require a a rebuild of framework as well? it is a hack but so is IE.

Mohammad
Unfortunately, it doesn't work. It remains write-only even after I write to it.
gustafc
+1  A: 

Can you use jQuery? There's a nice plugin that allows you to do window.postMessage through an iframe in IE 6-8: http://benalman.com/code/test/js-jquery-postmessage/

You could open your popup from the iframe and pass your object between iframe and parent with postMessage.

Will Peavy
This is probably my best shot. Thanks.
gustafc