兩個Web應用程序設置在IE9方面:使用JavaScript的window.open 「SCRIPT5:訪問被拒絕」 的一個子域beforeunload
david.mydomain.com
和
john.mydomain.com
David打開一個新窗口to John:
var popup = window.open('john.mydomain.com');
大衛想知道何時約翰會關閉然後發送XHR。
完成:
- 設置這樣正確的事件(即https://msdn.microsoft.com/library/ms536907(v=vs.85).aspx) :
john.attachEvent( 「onbeforeunload」,willClose);
- 設置相同的域在這樣每個窗口(即Cross-domain JavaScript code with sibling sub-domains):
$(文件)。就緒(函數(){window.document.domain = 'mydomain.com';}
- 並添加XHR發送(即https://www.w3schools.com/xml/tryit.asp?filename=try_dom_xmlhttprequest):
新的XMLHttpRequest()打開( 「GET」, 「welcome.png」,真)。發送();
我的代碼看起來終於想:
var willClose = function(e){
\t \t console.log('will close popup');
\t \t var xhttp = new XMLHttpRequest();
\t \t xhttp.onreadystatechange = function() {
\t \t \t if (this.readyState == 4 && this.status == 200) {
\t \t \t \t console.log('close xhr done');
\t \t \t }
\t \t };
\t \t xhttp.open("GET", "welcome.png", true);
\t \t xhttp.send();
\t \t return true ;
\t };
$(document).ready(function() {
\t \t
\t \t window.document.domain = 'mydomain.com';
\t \t
\t \t $('#popup').click(function() {
\t \t \t var win = window.open('http://john.mydomain.com/', '_blank');
\t \t \t
\t \t \t //KO : win.onbeforeunload = willClose;
\t \t \t //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>
問:
我仍然有一個IE跨域異常:「SCRIPT5:訪問是否認' on 1)john.attachEvent(「onbeforeunload」,willClose); 爲什麼?
(optionnal)由於優化這個js對所有的瀏覽器(IE9的會死)
更新到更新版本的jQuery? – evolutionxbox
Afaik,john無法調用在david中定義的函數,因爲它們不具有相同的範圍。是否有可能讓john再次打開一個超鏈接,包括像查詢字符串或散列,你可以在大衛代碼中查看並在IE9上工作?或者是約翰做敏感的工作,不能被欺騙。 (像登錄或類似的) – Shilly
這個錯誤似乎在jQuery版本1.10.2及更高版本中得到修復。 http://stackoverflow.com/a/17371187/989920 – evolutionxbox