addEventListener IE中的问题

时间:2012-07-16 09:07:50

标签: javascript jquery javascript-events onbeforeunload onunload

  

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

我正在尝试在其父页面调用的子弹出窗口上侦听close事件。我们的想法是让用户能够填写表单,然后使用弹出窗口接受权限并将视频上传到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方法。

您的帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

IE使用attachEvent而不是addEvent。

例如,参见theese线程 MSIE and addEventListener Problem in Javascript?addEventListener in Internet Explorer

答案 1 :(得分:1)

IE< 9不支持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的答案。