0

Possible Duplicate:
MSIE and addEventListener Problem in Javascript?

I am trying to listen for a close event on a child popup window that is called by its parent page. The idea is for a user to be able to fill out a form and then using a popup window accept permissions for and upload a video to YouTube.

The function I currently have works with Chrome but I cannot seem to get it to work on 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;
}

The idea is that once the popup has closed I can submit a form on the parent page.

The error I get is:

'Object doesn't support this property or method'

which I guess means that me assigning the created window does not support me calling the addEventListener method.

Your help with this would be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Neil Young
  • 556
  • 10
  • 23
  • 3
    `win.attachEvent('onunload', VideoUploaded);` – Rob W Jul 16 '12 at 09:09
  • Thank you very much for your prompt response, that solved my issue. – Neil Young Jul 16 '12 at 09:11
  • https://developer.mozilla.org/en/DOM/element.addEventListener#Legacy_Internet_Explorer_and_attachEvent – Quentin Jul 16 '12 at 09:18
  • Thank you for your answers guys. I now have a problem where when the window is opened for w3c (addEventListener) then the VideoUploaded function is executed before the window is even closed! This now does not happen on IE! Any ideas? – Neil Young Jul 16 '12 at 09:42

2 Answers2

1

IE uses attachEvent instead of addEvent.

For example see theese threads MSIE and addEventListener Problem in Javascript? and addEventListener in Internet Explorer

Community
  • 1
  • 1
Andreas
  • 2,336
  • 2
  • 28
  • 45
1

IE < 9 doesn't support addEventListener instead use 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);

Here is an answer on SO.

Community
  • 1
  • 1
The Alpha
  • 143,660
  • 29
  • 287
  • 307