addEventListener在IE 11中不起作用 [英] addEventListener does not work in IE 11

查看:1663
本文介绍了addEventListener在IE 11中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript打开弹出窗口,并在加载后执行一些代码.

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

这是代码:

// Ö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中工作.

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

据我了解,IE9以上版本应该支持addEventListener,因此从理论上讲IE 11应该支持它-但是似乎并非如此.

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.

此错误表明IE11不支持该方法...

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

显然,这应该根据其他线程来工作,但事实并非如此.如果使用IE,浏览器会转到else,但是它仍然无法正常工作.

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.

是不是IE中的attachEvent在弹出窗口上不起作用?

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

我刚刚尝试了第一个答案中指出的方法.

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

它可以在Firefox和Chrome中运行,但是IE拒绝运行,即使这种方法不再具有EventListener也是如此:

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):

我已将这一行代码添加到POPUP的HTML中:

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

这是javascript方面的更改:

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

在firefox和Chrome上,它可以完美运行,打开弹出窗口,并显示应该在弹出窗口上绘制的图表.

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

现在IE也执行弹出窗口的代码,这非常好,但是现在仅对于IE JQPLOT确实在库中的某个地方抛出了错误.

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.

我不知道为什么会这样,我只能猜测当执行jqplot的代码时弹出窗口不是喷射完成的.

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.

现在一切正常-jqplot的问题现已修复...

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

推荐答案

听起来像但是我看不到您问题的具体答案.

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

但是为什么不这样做

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

尝试一下

在Chrome Edge,IE11和FX中经过测试并可以正常工作(允许弹出窗口后)

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

这篇关于addEventListener在IE 11中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆