Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading window.location after setting document.domain in IE(6)

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.

like image 493
gustafc Avatar asked Nov 14 '22 14:11

gustafc


1 Answers

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.

like image 113
Will Peavy Avatar answered Nov 23 '22 22:11

Will Peavy