【问题标题】:addEventListener Problems in IE [duplicate]IE中的addEventListener问题[重复]
【发布时间】:2020-02-25 23:31:46
【问题描述】:

可能重复:
MSIE and addEventListener Problem in Javascript?

我正在尝试在其父页面调用的子弹出窗口上侦听关闭事件。这个想法是让用户能够填写表格,然后使用弹出窗口接受权限并将视频上传到 YouTube。

我目前拥有的功能适用于 Chrome,但我似乎无法让它在 IE 8 上运行?

function ShowUploadVideoPopUp(URL){
    //get the top and left position that the popup should be placed in
    //half the screen width and height to center the popup
    var top = Math.max(0, (($(window).height()) / 2) + $(window).scrollTop()) - 210;
    var left = Math.max(0, (($(window).width()) / 2) + $(window).scrollLeft()) - 300;
    //generate an id for this popup
    day = new Date();
    id = day.getTime();
    //open the window
    var win = window.open(URL, id, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=420,left = ' + left + ',top = ' + top);
    //we need to set a timeout otherwise the unload event is called before the window is opened
    //dont ask me why!?!
    setTimeout(function(){
        //add an event listener to listen for closure of the popup window
        //call the video uploaded function when the window is closed
        win.addEventListener("unload", VideoUploaded, false);
    }, 500);
    return false;
}

这个想法是,一旦弹出窗口关闭,我就可以在父页面上提交表单。

我得到的错误是:

'对象不支持该属性或方法'

我猜这意味着我分配创建的窗口不支持我调用 addEventListener 方法。

我们将不胜感激。

【问题讨论】:

  • win.attachEvent('onunload', VideoUploaded);
  • 非常感谢您的及时回复,解决了我的问题。
  • 谢谢你们的回答。我现在有一个问题,当为 w3c (addEventListener) 打开窗口时,在窗口关闭之前执行 VideoUploaded 函数!这现在不会发生在 IE 上!有什么想法吗?

标签: javascript jquery dom-events onbeforeunload onunload


【解决方案1】:

IE addEventListener 而是使用attachEvent

setTimeout(function(){
    if(win.addEventListener) // w3c standard
        win.addEventListener("unload", VideoUploaded, false);
    else if win.attachEvent('onunload', VideoUploaded, false); // IE
    else win.onunload=VideoUploaded;
}, 500);

这是SO的答案。

【讨论】:

    【解决方案2】:

    IE 使用 attachEvent 而不是 addEvent。

    例如查看这些线程 MSIE and addEventListener Problem in Javascript?addEventListener in Internet Explorer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-15
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2011-10-24
      • 2012-07-10
      相关资源
      最近更新 更多