Reputation: 934
I am not sure what is going on but IE's window.open works horribly!
I have a piece of javascript code that gets called from an angular controller that opens a second window (within same domain, popup blocked turned off) and tries to call a javascript method on that second window.
My code:
var newWindow = $window.open('somefile.html?' + Math.random(1000));
var fn = function () {
//alert('I got called');
newWindow.display($scope.mymsg);
}
if (newWindow.addEventListener)
newWindow.addEventListener("load", fn, false);
else if (newWindow.attachEvent)
newWindow.attachEvent("onload", fn);
else
newWindow.onload = fn;
I need to support IE8 and IE9. I fiddled around with the code and got it work on IE8, but I cant get it to work on IE9.
If I uncomment the alert, in IE9, i never see this. I have no idea why, since i am attaching the onload handler correctly. I am suspecting there is some sort of race condition, but if that was the case, I would have problems in the new window's display(), but it does not even get there because onload does not get called.
UPDATE: So playing around with wrapping newly created window with jQuery helped a lot:
var newWindow = $window.open('somefile.html?z=' + Math.random(1000));
$(newWindow).load(function () { this.display($scope.mymsg); });
Also I added the dummy "z" param which I missed earlier. I almost have it working.
Right now it seems like when you open this page for the first time, and quickly click the button to open second window, the second window opens up blank in IE8 (where it should have some messaged that were passed to it). But if you open the page and wait a couple of seconds the second window opens correctly displaying the passed in message.
I also added a $(document).ready() in the second window's display() method.
Another thing I did not mention before, but I think might have something to do with this, is that the whole mechanism which opens this window is wrapped inside a $q.all(). This is because before I open second window i make a call to save the data and return validation messages. I wonder if there is something going on with q, that is causing this race condition.
UPDATE2: Moved the code to the last promise in the promise chain in $q. So right now it looks like this:
$q.all([
//load data and put validate errors into $scope.mymsg
]).then(
function ()
{
$(document).ready(function () {
var newWindow = $window.open('somefile.html?z=' + Math.random(1000));
$(newWindow).load(function () { this.display($scope.mymsg); });
});
},
function ()
{
}
);
Issue still happening but a lot of less. It is definitely a race condition, but where I cannot figure it out. If you open certain pages and click on a button to open second window without waiting for page to fully load, it still might happen in certain random situations.
Upvotes: 0
Views: 1826
Reputation: 866
It looks like when fn
gets attached to the scripting sandbox of newWindow
, then the newWindow
variable in the parent is out of scope when fn
actually gets executed there in the child.
Have you tried something like this in the parent:
popupParameters = {
foo: 'bar'
};
and this in a script
tag in the child:
popupParameters = window.opener.popupParameters;
alert(popupParameters.foo);
window.opener
should work in IE 8 and 9 ... I think.
Upvotes: 3