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');
}
4

2 回答 2

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');
        };
    }
}
于 2012-05-24T07:32:22.923 回答
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')"
于 2012-05-24T07:38:49.580 回答