Using Javascript Objects in Separate IE Windows

I need to open a new browser window from another browser window and access the object from the parent window in the child window. Therefore, when the child window loads, I use the openener property to access the object from the parent. Works well in Firefox, but in IE, array properties are converted to objects.

eg.

function openChild() { window.open(window.document.location, '_blank'); } var data = { myArray: [] }; $(document).ready(function() { alert('data is array: ' + (data.myArray instanceof Array)); alert('prototype: ' + (Object.prototype.toString.call(data.myArray))); if (window.opener) { var parentData = window.opener.data; alert('parent data is array: ' + (parentData.myArray instanceof Array)); alert('parent prototype: ' + (Object.prototype.toString.call(parentData.myArray))); } }); 

When the child window opens in IE, the result will be

 data is array: true prototype: [object Array] parent data is array: false parent prototype: [object Object] 

and the result in Firefox is

 data is array: true prototype: [object Array] parent data is array: false parent prototype: [object Array] 

One job too serializes the object in JSON, passes the string, and then deserializes. However, any methods of the object are lost.

What else can I do except sit around talking about how IE is the scourge of web development?

+3
source share
2 answers

One solution is to convert the object to JSON in the parent window and pass the string to the child, which then parses the JSON back into the object.

eg. In the parent window:

 function getData() { return JSON.stringify(data); } 

and in the child window:

 var parentData = JSON.parse(window.opener.getData()); 

However, this will lose any methods for any objects.

+2
source

Today I ran into a similar problem and found the following solution using underscore.js worked well:

var parentData = _.toArray(window.opener.data);

0
source

Source: https://habr.com/ru/post/951403/


All Articles