简体   繁体   中英

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;

This work well in FF, but in IE (6/7) the data can be accessed for the time the popup is still displayed. 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 :

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.

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.

do you have a URL or some more source code that you can provide?

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.

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

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.

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 !

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. :-(

The way I finally did it is by json encoding the complex object I wanted to pass to the parent in the popup window. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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