Javascript Clone Array Internet Explorer

I have a strange error in Internet Explorer when trying to clone / copy and array from a window to another window.

Window A
 has an array called arr1
 opens Window B using javascript

Window B
 has an array called arr2
 changes the value of arr1 to arr2 using window.opener.arr1 = arr2;

Works fine until you close window B and then Window A arr1 gives an javascript error 'the callee server not server application is not available and disappeared' when you try and access arr1

I understand that arr1 might be a pointer to arr2 so when i close Window B it loses this reference, but this is only the case in Internet Explorer.

I have tried many methods to try and copy / clone the array but they all have the same issue when i close window B.

x2cmsacAsked:
Who is Participating?
 
StingRaYCommented:
Try this

function cloneObject(o) {
  for (i in o)
  {
    t = typeof(o[i]);
    if ((typeof(o[i].length) === 'number') && !(o[i].propertyIsEnumerable('length')) && (typeof(o[i].splice) == 'function'))
      t = 'array';
    switch(t)
    {
      case 'object':
        this[i] = new cloneObject(o[i]);
        break;
      case 'array':
        this[i] = new Array();
        for (j = 0; j < o[i].length; j++)
        {
          if (typeof o[i][j] == 'object')
            this[i].push(new cloneObject(o[i][j]));
          else
            this[i].push(o[i][j]);
        }
        break;
      default:
        this[i] = o[i];
    }
  }
}

This function will clone your object to another variable.

For example.

var a = [[1, 2, 3], ["a", "b"], "text", {p:"prop",q:"queue"}];
var b = new cloneObject(a);

function retrieveObject(o) {
      var output = '[\n';
      for (i in o) {
            if (typeof o[i] == 'object')
                  output += retrieveObject(o[i]);
            else
                  output += i + ' = ' + o[i] + '\n';
      }
      output += "]\n";
      return output;
}

a[2] = "value";
alert(retrieveObject(a));
alert(retrieveObject(b));
0
 
ZvonkoSystems architectCommented:
Check this:

for(var i=0;i<arr2.length;i++){
  window.opener.arr1[i] = arr2[i];
}
arr1.length = arr2.length;





0
 
x2cmsacAuthor Commented:
doesnt work when you use multi dimensional arrays? I need a solution which will work for both.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
ZvonkoSystems architectCommented:
I have not tested this but I assume that it depends in what window context you did the array copy.
Check this:

window.opener.arr1 = [];
window.opener.arr1 = window.opener.arr1.concat(arr2);

0
 
MageUKCommented:
function clone (obj, deep) {
  var cloned = new Object();
  if(!obj) return cloned;
  for (var key in obj)
    if (!deep)
      cloned[key] = obj[key];
    else if (typeof obj[key] == 'object')
      cloned[key] = clone(obj[key], deep);
    else
      cloned[key] = obj[key];
  return cloned;
}
0
 
LeeKowalkowskiCommented:
Have you tried serialisation?  I.e., write a function to convert this array into a String (serialise), and a function to create an array from its serial form.  

Perhaps not the most elegant solution, but definitely easy to maintain - and will guarantee no references to a dead window object.

--
Lee
0
 
Ersoy HasanCommented:
This is the correct way to clone an array in javascript:
var someArray = ["1", "test", 0 , [2,3]];
var s1 = someArray.slice(0);//here is the actual cloning
s1[0] = 1 + 3;
alert(s1[0]);
alert(someArray[0])

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.