0

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 :

  1. setting the right event like this (ie https://msdn.microsoft.com/library/ms536907(v=vs.85).aspx ) :

    john.attachEvent("onbeforeunload", willClose);

  2. 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';}

  3. 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)

Community
  • 1
  • 1
Mat Cloud
  • 123
  • 1
  • 1
  • 6
  • Update to a more recent version of jQuery? – evolutionxbox Mar 09 '17 at 14:02
  • 2
    Afaik, john can't call functions defined in david, since they don't share the same scope. Is it possible to have john open a hyperlink to david again, including like a query string or a hash, something you can check inside the david code and that works on IE9? Or is john doing sensitive work that can't be spoofed. (Like a login or such) – Shilly Mar 09 '17 at 14:02
  • This error seems to be fixed in jQuery versions 1.10.2 and up. http://stackoverflow.com/a/17371187/989920 – evolutionxbox Mar 09 '17 at 14:03
  • @evolutionxbox : still KO with jQuery 1.10.2. – Mat Cloud Mar 09 '17 at 14:25

1 Answers1

-1

According to the documentation: window.document.domain : Get (no Set) the domain name of the server that loaded the document.enter link description here

Community
  • 1
  • 1
funcoding
  • 741
  • 6
  • 11