2012-05-24 73 views
0

我想打開一個窗口window.open並附加一個函數,它的onload。例如,當子頁面加載完成(僅DOM)時,瀏覽器在子頁面中顯示警報。 此代碼不起作用::兒童負載提醒

var google; 
function google(){ 
    google = window.open('http://www.google.com/'); 

    // Dean Edwards/Matthias Miller/John Resig 

    /* for Mozilla/Opera9 */ 
    if (google.document.addEventListener){ 
     google.document.addEventListener("DOMContentLoaded", pageLoaded, false); 
    } 

    /* for Internet Explorer */ 
    /*@cc_on @*/ 
    /*@if (@_win32) 
     google.document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>"); 
     var script = google.document.getElementById("__ie_onload"); 
     script.onreadystatechange = function(){ 
     if (this.readyState == "complete") { 
      pageLoaded(); // call the onload handler 
     } 
     }; 
    /*@end @*/ 

    /* for Safari */ 
    if (/WebKit/i.test(navigator.userAgent)){ // sniff 
     var _timer = setInterval(function(){ 
      if (/loaded|complete/.test(google.document.readyState)) { 
       pageLoaded(); // call the onload handler 
      } 
     }, 10); 
    } 

    google.onload = pageLoaded; 
} 
function pageLoaded(){ 
    alert('Loading Completed'); 
} 
+2

同源-政策。您無法修改外部域文檔的代碼 - 甚至無法將腳本注入其中。 – ThiefMaster

回答

0

試試這個:

var google; 
function googleload() 
{ 
    google = window.open('http://www.google.com/'); 

    // Dean Edwards/Matthias Miller/John Resig 

    /* for Mozilla/Opera9 */ 
    if (google.document.addEventListener) 
    { 
      google.window.addEventListener("DOMContentLoaded", function(){ 
       alert('Loading Completed'); 
      }, false); 
    } 
    else 
    { 
     google.window.onload = function(){ //Add window.onload 
      alert('Loading Completed'); 
     }; 
    } 
} 
0

如前所述同源策略是在用自己的方式

這裏是我會嘗試,如果我真的有到

function popAndAlert(url) { 
    var w = window.open('','mywindow'); 
    if (w) { 
    w.document.write('<body onload="opener.done()">'+ 
     '<iframe style="width:100%;height:100%" src="'+url+'"></iframe></body>') 
    w.document.close(); 
    } 
} 
function done() { 
    alert('loading completed'); 
} 

在子頁面提醒,改變

onload="opener.done()" 

onload="alert('loading complete')"