Two web applications setups in an IE9 context :
david.mydomain.com
and
john.mydomain.com
David opens a new window to John :
var popup = window.open('john.mydomain.com');
And david wants to know when john will close and then sending an XHR.
Done :
- setting the right event like this (ie https://msdn.microsoft.com/library/ms536907(v=vs.85).aspx )
:
john.attachEvent("onbeforeunload", willClose);
- setting the same domain in each window like this ( ie Cross-domain JavaScript code with sibling sub-domains ) :
$(document).ready(function() {window.document.domain = 'mydomain.com';}
- and adding XHR sent (ie https://www.w3schools.com/xml/tryit.asp?filename=try_dom_xmlhttprequest ):
new XMLHttpRequest().open("GET", "welcome.png", true).send();
My code looks finally like :
var willClose = function(e){
console.log('will close popup');
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log('close xhr done');
}
};
xhttp.open("GET", "welcome.png", true);
xhttp.send();
return true ;
};
$( document ).ready(function() {
window.document.domain = 'mydomain.com';
$('#popup').click(function() {
var win = window.open('http://john.mydomain.com/', '_blank');
//KO : win.onbeforeunload = willClose;
//KO : win.addEventListener ("beforeunload", willClose);
win.attachEvent("onbeforeunload", willClose);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<button id="popup">Open pop up in sub domain</button>
Question :
I still got an IE cross-domain exception: 'SCRIPT5: Access is denied' on 1) john.attachEvent("onbeforeunload", willClose); Why ?
(optionnal) Thanks to optimize this js for all browsers (IE9's gonna die)