簡體   English   中英

addEventListener 在 IE 11 中不起作用

[英]addEventListener does not work in IE 11

我正在使用 javascript 打開一個彈出窗口並在加載后執行一些代碼。

這是代碼:

// Öffnen des Print Popups binden.
$('#revi_print').unbind();
$('#revi_print').click(function() {

    // Popup erstellen.
    popup = window.open('report_handle/print.php?filter_report=' + $('#revi').data('filter_report'), "Popup", "width=1024, height=768, scrollbars=yes, toolbar=no, status=no, resizable=yes, menubar=no, location=no, directories=no, top=10, left=10");

    // Code erst ausführen, wenn das Popup geladen ist.
    popup.addEventListener('load', handle_popup, false);
});

它在 Firefox 和 Google Chrome 中運行良好,但是我意識到它在最新的 Internet Explorer 中不起作用。

從我讀到的內容來看,應該在 IE9 以上支持 addEventListener,所以理論上 IE 11 應該支持它 - 但似乎情況並非如此。


此錯誤表明 IE11 不支持該方法...

在此處輸入圖片說明


是否有一個簡單的解決方法來完成這項工作?


我剛剛試過這段代碼:

if (popup.addEventListener){
    alert("firefox, chorome, etc");
    popup.addEventListener('load', handle_popup, false); 
} else if (popup.attachEvent){
    alert("IE");
    popup.attachEvent('load', handle_popup);
}   

顯然這應該根據不同的其他線程工作,但事實並非如此。 當使用 IE 時,瀏覽器確實會轉到 else if - 但是它仍然拒絕工作。

會不會,IE 中的 attachEvent 對彈出窗口不起作用?

在此處輸入圖片說明


我剛剛嘗試了第一個答案中指出的方法。

它適用於 firefox 和 chrome,但 IE 拒絕工作,即使這種方法不再有 EventListener:

// Öffnen des Print Popups binden.
$('#revi_print').unbind();
$('#revi_print').click(function() {

    // Popup erstellen.
    popup = window.open('report_handle/print.php?filter_report=' + $('#revi').data('filter_report'), "Popup", "width=1024, height=768, scrollbars=yes, toolbar=no, status=no, resizable=yes, menubar=no, location=no, directories=no, top=10, left=10");

    // Code erst ausführen, wenn das Popup geladen ist.
    //popup.addEventListener('load', handle_popup, true);

    popup.window.onload=function() { parent.handle_popup(popup); }
});

// Code zum handeln des Popups.
function handle_popup(popup) {
    var selected_report = $('#revi').data('filter_report');
    var jqplot_object = $('#revi_filter_ddReport_' + selected_report + '_jqplot_object').html();
    var has_chart = $('#revi_filter_ddReport_' + selected_report + '_has_chart').html();
    var obj = $.parseJSON($('#revi').data('data').trim());

    // Den Kontent kopieren.
    popup.$('#revi_sec_report_container').html($('#revi_report_container').html());

    // Den Print Button entfernen.
    popup.$('#revi_print').remove();

    // Das chart entfernen.
    popup.$('#revi_chart').empty();

    // Wenn ein Chart gezeichnet werden soll.
    if (has_chart == 1) { 
        var execute_string = $.base64.decode(jqplot_object);
        eval(execute_string); 
    }
}

下一次嘗試(成功一半):

我已將這行代碼添加到 POPUP 的 HTML 中:

在此處輸入圖片說明

這是 javascript 方面的變化:

// Öffnen des Print Popups binden.
$('#revi_print').unbind();
$('#revi_print').click(function() {

    // Popup erstellen.
    popup = window.open('report_handle/print.php?filter_report=' + $('#revi').data('filter_report'), "Popup", "width=1024, height=768, scrollbars=yes, toolbar=no, status=no, resizable=yes, menubar=no, location=no, directories=no, top=10, left=10");

    $('body').data('the_popup', popup);

    // Code erst ausführen, wenn das Popup geladen ist.
    //popup.addEventListener('load', handle_popup, true);

    //window.onload=function() { handle_popup(popup); }
});

// Code zum handeln des Popups.
function handle_popup() {

    var popup = $('body').data('the_popup');

    var selected_report = $('#revi').data('filter_report');
    var jqplot_object = $('#revi_filter_ddReport_' + selected_report + '_jqplot_object').html();
    var has_chart = $('#revi_filter_ddReport_' + selected_report + '_has_chart').html();
    var obj = $.parseJSON($('#revi').data('data').trim());

    // Den Kontent kopieren.
    popup.$('#revi_sec_report_container').html($('#revi_report_container').html());

    // Den Print Button entfernen.
    popup.$('#revi_print').remove();

    // Das chart entfernen.
    popup.$('#revi_chart').empty();

    // Wenn ein Chart gezeichnet werden soll.
    if (has_chart == 1) { 
        var execute_string = $.base64.decode(jqplot_object);
        eval(execute_string); 
    }
}

在 firefox 和 Chrome 上它完美運行,彈出窗口打開,應該在彈出窗口上繪制的圖表顯示出來。

現在IE也執行popup的代碼,非常好,但是現在只針對IE JQPLOT確實會在庫的某個地方拋出錯誤。

我不知道為什么會發生這種情況,我只能猜測當 jqplot 的代碼被執行時,彈出窗口沒有完成加載。


現在一切正常 - jqplot 的事情現在已經修復了......

聽起來像是檢測用 window.open 打開的窗口的 onload 事件的騙局

但我看不到你的問題的具體答案。

但為什么不做

window.onload=function() { opener.handle_popup() } // or attachEventListener

在子窗口? 不需要可能永遠不會觸發的附加事件,因為您的附加可能是在負載觸發之后

試一試

在 Chrome Edge、IE11 和 FX 中測試和工作(允許彈出窗口后)

正如“ https://github.com/angular/zone.js/issues/535 ”中所解釋的,另一個原因有時是IE需要時間來返回window.open返回的窗口對象。 因此,當您的addEventlistener代碼執行時,該對象沒有此功能。 我使用的解決方法是:

popup = window.open('report_handle/print.php?filter_report=' + $('#revi').data('filter_report'), "Popup", "width=1024, height=768, scrollbars=yes, toolbar=no, status=no, resizable=yes, menubar=no, location=no, directories=no, top=10, left=10");
var interval = setInterval(function(){
   if(typeof popup.addEventListener === "function"){
   clearInterval(interval);
    popup.addEventListener('load', handle_popup, false);
   }
},100);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM