简体   繁体   English

Javascript从子窗口传递数据到父窗口,IE bug?

[英]Javascript passing data from child window to parent window, IE bug?

I've got a popup window that gives data back to its parent. 我有一个弹出窗口,将数据提供给其父级。 Using window.opener.document.data = data_from_popup; 使用window.opener.document.data = data_from_popup;

This work well in FF, but in IE (6/7) the data can be accessed for the time the popup is still displayed. 这在FF中运行良好,但在IE(6/7)中,可以在弹出窗口仍然显示的时候访问数据。 When I close the popup it looks like the data gets garbage collected. 当我关闭弹出窗口时,看起来数据被垃圾收集。

I've tried to use a clone() function for the data received from the popup : 我试图使用clone()函数来获取从弹出窗口接收的数据:

window.opener.add_data(data_from_popup);

and in the parent : 在父母:

function add_data(data_from_popup) {
 data = clone(data_from_popup); 
}

It somewhat works, but in certain conditions the clone() function seems to recurse infinitely. 它有点工作,但在某些条件下, clone()函数似乎无限递归。

Have you ever experienced the same thing, and is there a way to prevent that without using a clone function? 您是否经历过相同的事情,有没有办法在不使用克隆功能的情况下阻止它?

Not sure exactly what you are experiencing, but I have successfully stored data on the opener from the child popup on a regular basis in IE (6,7 & 8) in development and production applications. 不确定您正在经历什么,但我已经成功地在开发和生产应用程序中的IE(6,7和8)中从子弹出窗口中成功地将数据存储在开启器上。

do you have a URL or some more source code that you can provide? 你有一个URL或一些你可以提供的源代码吗?

on a related note... you aren't trying to determine the type of an object on the opener... from the popup are you? 在一个相关的说明...你不是试图确定开启者对象的类型...从弹出窗口你是吗? - there's some known IE bugs in this area. - 这个领域有一些已知的IE漏洞。

Update: 更新:

Here's a quick example... 这是一个简单的例子......

<!--main window-->
<script>
  function getThing(id){
    url = 'http://mysite.com/...whatever...';
    features = 'locationbar=X,menubar=Y...';
    window['popup4'+id] = open(url, 'someNameWithoutSpaces', features);
  }
  function popupCallback(data){
    alert('I got data back! ' + data);
    document.getElementById('someID').innerHTML = '<b>' + data.label + ' (' + data.id + ')</b>';
    //close the popup (now that we have/are done with the data)
    window['popup4someID'].close();
  }
</script>
<div id="someID">{Nothing Selected Yet}</div>
<input type="button" value="Pick One" onclick="getThing('someID');"/>

<!--popup window-->
<script>
  function saveSelection(){
    //acquire data however you want/need
    var selData = {};
    selData.id = 123456;
    selData.label = 'iPod Touch (Jeff Atwood Edition)';
    //call callback on opener
    window.opener.popupCallback(selData);
  }
</script>

Update 2 更新2

In testing it appears that in IE7,IE8 (but not IE6) after the popup window is closed, any reference data is lost (the references don't capture a snapshot) thus if you need the data after the popup is closed, you will need to clone it. 在测试中看来,在IE7,IE8(但不是IE6) 弹出窗口关闭后,任何参考数据都会丢失 (引用不会捕获快照),因此如果在弹出窗口关闭后需要数据,你将会需要克隆它。

I thought if the data can be wrapped in an Array, cloning is a piece of cake. 我想如果数据可以包装在一个数组中,克隆就是小菜一碟。 Just call .slice() on it to copy it, but... that doesn't work either ! 只需调用它上面的.slice()来复制它, 但是......这也不起作用

I guess you'll need to save the values out that you need (either to form elements, or the DOM) since IE doesn't look like it will let you use them after the popup closes. 我猜你需要保存你需要的值(要么形成元素,要么是DOM),因为IE看起来不会让你在弹出窗口关闭后使用它们。 :-( :-(

The way I finally did it is by json encoding the complex object I wanted to pass to the parent in the popup window. 我最终做到的方式是通过json编码我希望传递给弹出窗口中的父对象的复杂对象。 The data passed back is then a simple string and can be copied without problem. 传回的数据是一个简单的字符串,可以毫无问题地复制。 On the parent side the json encoded string is evaluated to a Javascript object. 在父端,json编码的字符串被评估为Javascript对象。

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

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