gpt4 book ai didi

javascript - addEventListener 在 IE 11 中不起作用

转载 作者:行者123 更新时间:2023-11-30 14:54:14 35 4
gpt4 key购买 nike

我正在使用 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 中不起作用。

据我所知,addEventListener 应该在 IE9 以上支持,所以理论上 IE 11 应该支持它 - 但似乎并非如此。


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

enter image description here


是否有一个简单的解决方法来完成这项工作?


我刚刚试过这段代码:

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 - 但是它仍然拒绝工作。

难道 IE 中的 attachEvent 不适用于弹出窗口?

enter image description here


我刚试过第一个答案中的方法。

它在 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 中:

enter image description here

这是 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 也执行了弹出窗口的代码,这很好,但现在仅适用于 IE JQPLOT 确实会在库中的某处抛出错误。

我不知道为什么会这样,我只能猜测当执行 jqplot 的代码时弹出窗口没有完成加载。


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

最佳答案

听起来像 Detecting the onload event of a window opened with window.open 的骗局

但是我在里面看不到你问题的具体答案。

但为什么不这样做

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

在子窗口?不需要可能永远不会触发的附加事件,因为您的附加可能在加载触发之后

TRY IT

在 Chrome Edge、IE11 和 FX 中测试并工作(允许弹出窗口后)

关于javascript - addEventListener 在 IE 11 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47589796/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com