最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - addEventListener does not work in IE 11 - Stack Overflow

programmeradmin1浏览0评论

I am using javascript to open a popup and execute some code once it is loaded.

This is the code:

// Ö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);
});

It does work fine in Firefox and Google Chrome, however I have realized, that it does not work in the newest Internet Explorer.

From what I have read, addEventListener should be supported above IE9, so theoretically IE 11 should support it - however it seems this is not the case.


This error indicates, that IE11 is not supporting the method...


Is there a simple workaround to make this work?


I have just tried this pice of code:

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

Apparently this should work according to different other threads, but it is not the case. The browser does go to the else if, when IE is used - however it still refuses to work.

Could it be, that attachEvent in IE does not work on popups?


I have just tried the method indicated in the first answer.

It works in firefox and chrome, but IE refuses to work, even tough this method does not have the EventListener any more:

// Ö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); 
    }
}

Next attempt (half successful):

I have added this line of code to the HTML of the POPUP:

This are the changes on the javascript side:

// Ö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); 
    }
}

On firefox and Chrome it works perfectly, the popup opens, and the chart that should be drawn on the popup shows up.

Now IE also executes the code for the popup, which is very good, but now only for IE JQPLOT does throw an error somewhere in the library.

I have no clue why this happens, I can only guess that the popup is not jet finished loading, when the code for jqplot is executed.


Got everything working now - the jqplot thing is fixed now...

I am using javascript to open a popup and execute some code once it is loaded.

This is the code:

// Ö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);
});

It does work fine in Firefox and Google Chrome, however I have realized, that it does not work in the newest Internet Explorer.

From what I have read, addEventListener should be supported above IE9, so theoretically IE 11 should support it - however it seems this is not the case.


This error indicates, that IE11 is not supporting the method...


Is there a simple workaround to make this work?


I have just tried this pice of code:

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

Apparently this should work according to different other threads, but it is not the case. The browser does go to the else if, when IE is used - however it still refuses to work.

Could it be, that attachEvent in IE does not work on popups?


I have just tried the method indicated in the first answer.

It works in firefox and chrome, but IE refuses to work, even tough this method does not have the EventListener any more:

// Ö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); 
    }
}

Next attempt (half successful):

I have added this line of code to the HTML of the POPUP:

This are the changes on the javascript side:

// Ö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); 
    }
}

On firefox and Chrome it works perfectly, the popup opens, and the chart that should be drawn on the popup shows up.

Now IE also executes the code for the popup, which is very good, but now only for IE JQPLOT does throw an error somewhere in the library.

I have no clue why this happens, I can only guess that the popup is not jet finished loading, when the code for jqplot is executed.


Got everything working now - the jqplot thing is fixed now...

Share edited Dec 1, 2017 at 14:01 Andreas asked Dec 1, 2017 at 8:58 AndreasAndreas 5553 gold badges11 silver badges25 bronze badges 5
  • Possible duplicate of addEventListener not working in IE8 – moon Commented Dec 1, 2017 at 9:05
  • perhaps you're in "patibility" mode – Jaromanda X Commented Dec 1, 2017 at 9:07
  • is partibility mode on per default? dont think so.. I just checked and it seems I have to add pages to partibility view - to me it looks as it is off per default. – Andreas Commented Dec 1, 2017 at 9:12
  • why not $(popup.document).ready( handle_popup )? – gurvinder372 Commented Dec 1, 2017 at 9:39
  • Please change popup.window.onload=function() { parent.handle_popup(popup); } to popup.window.onload=function() { opener.handle_popup(popup); } – mplungjan Commented Dec 1, 2017 at 12:47
Add a ment  | 

2 Answers 2

Reset to default 3

Sounds like a dupe of Detecting the onload event of a window opened with window.open

but I could not see a specific answer of your question in it.

But why not do

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

in the child window? Not need for attach events that may never be triggered because your attaching may be after the load triggered

TRY IT

Tested and working (after allowing popups) in Chrome Edge, IE11 and FX

As explained in "https://github./angular/zone.js/issues/535", another reason sometimes is that IE takes time to return the window object returned by window.open. As a result by the time your addEventlistener code executes, the object doesn't have this function. The workaround I used was:

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);
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>