1

I'm working in IE11

I've a code that opens a new window and adds a eventListener on new window for load event.

var newWindow = window.open(someURL, ..., ...);
newWindow.addEventListener('load', callback, true);
// when IE11 executes this line, it throws error "object doesn't support  property or method".

When I open debugger to the line of issue, the newWindow object has addEventListener method listed in its list of methods.

Can someone advise or explain the possible reason for the behavior in IE11.

Some research - I found an issue where newWindow doesn't allow you to use appendChild with element from parent window. appendChild not working with window.open in IE

Can similar thing be an issue?

Community
  • 1
  • 1

1 Answers1

1

I ran into this issue on a recent project. It seems like IE needs a bit of time to properly initialize the new window, and the new window object does not yet have the addEventListener method immediately after opening. Only IE does this; I have been unable to observe this behavior in any other browser. I can also confirm that newWindow.onload is pretty much useless.

Here is how I worked around it:

var newWindow = window.open();

// wait until addEventListener method is available to window
var windowInitialized = false;
while (windowInitialized === false) {
    if (typeof newWindow.addEventListener === 'function') {
        windowInitialized = true;
    }
}

// from here on you can safely use newWindow.addEventListener
Brother Woodrow
  • 5,058
  • 3
  • 13
  • 17