Javascript Clone Array Internet Explorer

Posted on 2007-07-25
Last Modified: 2013-12-12
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.

Question by:x2cmsac
    LVL 63

    Expert Comment

    Check this:

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


    Author Comment

    doesnt work when you use multi dimensional arrays? I need a solution which will work for both.
    LVL 63

    Expert Comment

    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);

    LVL 15

    Accepted Solution

    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';
          case 'object':
            this[i] = new cloneObject(o[i]);
          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]));
            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]);
                      output += i + ' = ' + o[i] + '\n';
          output += "]\n";
          return output;

    a[2] = "value";

    Assisted Solution

    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);
          cloned[key] = obj[key];
      return cloned;
    LVL 16

    Expert Comment

    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.

    LVL 4

    Expert Comment

    by:Ersoy Hasan
    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;

    Open in new window


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    AJAX post submit actions 6 240
    GridMvc Error mvc5 5 148
    Change value in a Datagrid 1 31
    Google forms auto fill based on text entered. 3 96
    If you're not part of the solution, you're part of the problem.   Tips on how to secure IoT devices, even the dumbest ones, so they can't be used as part of a DDoS botnet.  Use PRTG Network Monitor as one of the building blocks, to detect unusual…
    For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now