简体   繁体   English

IE9 window.open问题

[英]IE9 window.open problems

I am not sure what is going on but IE's window.open works horribly ! 我不确定发生了什么,但是IE的window.open太糟糕了
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. 我有一段javascript代码,这些代码是从角度控制器调用的,该角度控制器打开第二个窗口(在同一域中,弹出窗口已关闭),并尝试在该第二个窗口上调用javascript方法。

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. 我需要支持IE8和IE9。 I fiddled around with the code and got it work on IE8, but I cant get it to work on IE9. 我摆弄代码,并使其可在IE8上运行,但无法在IE9上运行。
If I uncomment the alert, in IE9, i never see this. 如果取消注释该警报,则在IE9中,我永远不会看到此消息。 I have no idea why, since i am attaching the onload handler correctly. 我不知道为什么,因为我正确地附加了onload处理程序。 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. 我怀疑存在某种竞争状况,但是如果真是这样,我将在新窗口的display()中遇到问题,但它甚至无法到达那里,因为不会调用onload。

UPDATE: So playing around with wrapping newly created window with jQuery helped a lot: 更新:因此,使用jQuery包装新创建的窗口很有帮助:

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. 另外,我还添加了我之前错过的虚拟“ z”参数。 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). 现在看来,当您第一次打开此页面并快速单击按钮以打开第二个窗口时,第二个窗口在IE8中打开为空白(应该在其中传递一些消息)。 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. 我还在第二个窗口的display()方法中添加了$(document).ready()。
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(). 我之前没有提到的另一件事,但我认为可能与此有关,是打开此窗口的整个机制都包装在$ 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. 我想知道q是否发生了某种情况,从而导致了这种竞争状况。

UPDATE2: Moved the code to the last promise in the promise chain in $q. UPDATE2:将代码移至$ q的Promise链中的最后一个Promise 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. 如果您打开某些页面并单击按钮以打开第二个窗口而不等待页面完全加载,则在某些随机情况下仍然可能会发生这种情况。

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. 看起来当fn附加到newWindow的脚本沙箱时,则当fn实际上在子级中的那里执行时,父级中的newWindow 变量不在范围内。

Have you tried something like this in the parent: 您是否在父母中尝试过类似的方法:

popupParameters = {
    foo: 'bar'
};

and this in a script tag in the child: 这在子代的script标记中:

popupParameters = window.opener.popupParameters;
alert(popupParameters.foo);

window.opener should work in IE 8 and 9 ... I think. window.opener应该可以在IE 8和IE 9中运行...我认为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM