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?
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.
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.
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