parent.CHILD_WINDOW = window;
parent.INIT_DD = parent.document.domain;
var ddPoll = function(){
window.DD_POLL=setInterval(function(){
var childDoc = window.CHILD_WINDOW.document;
if (document.domain !== window.INIT_DD) {
window.CHILD_WINDOW.postMessage({dd:document.domain}, '*');
clearInterval(window.DD_POLL);
}
}, 33);
};
var onMsg = function(e) {
if (e.data.dd) {
document.domain = e.data.dd;
}
};
if (window.addEventListener) {
window.addEventListener('message', onMsg);
} else {
window.attachEvent('onmessage', onMsg);
}
var runJS = function(doc, fn) {
var js = doc.createElement('script');
js.id = "script-" + (new Date());
js.text = "("+fn.toString() + ")();document.body.removeChild(document.getElementById('" +js.id +"'));";
doc.body.appendChild(js);
};
runJS(parent.document, ddPoll);
////below here is code to poll for the current window's document.domain, as well as the parent window.
var updateInfo = function(dd, pdd) {
var a = document.getElementById('a');
var result = '';
var log = function(msg) {
result += msg + '<br/>';
}
log('local: ' + dd);
log('parent: ' + pdd);
a.innerHTML = result;
}
var i = setInterval(function() {
var dd1 = "error", dd2 = "error";
try{dd1 = document.domain;}catch(e){}
try{dd2 = parent.document.domain;}catch(e){
clearInterval(i);
}
updateInfo(dd1, dd2);
}, 200);
<div id=a></div>
#a {
display: block;
background: lightblue;
width: 300px;
height: 130px;
}
body{
margin:0px;
padding:0px;
}