Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 308
mtourne Avatar asked Apr 24 '09 00:04

mtourne


People also ask

How do I pass a value from child window to parent window?

From a child window or a small window once opened, we can transfer any user entered value to main or parent window by using JavaScript. You can see the demo of this here. Here the parent window is known as opener. So the value we enter in a child window we can pass to main by using opener.

Why is window opener null?

If this window was not opened by being linked to or created by another, returns null . If the opener is not on the same origin as the current page, functionality of the opener object is limited. For example, variables and functions on the window object are not accessible.

How use JavaScript window opener?

The Window opener property in HTML DOM is used to return the reference of newly created windows. This property is used to return the details of the source (parent) window. A window is opened using the window. open() method and closed using the window.


1 Answers

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

like image 101
scunliffe Avatar answered Oct 27 '22 00:10

scunliffe