Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Javascript Clone Array Internet Explorer

Posted on 2007-07-25
9
Medium Priority
?
346 Views
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.

0
Comment
Question by:x2cmsac
7 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 19579500
Check this:

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





0
 

Author Comment

by:x2cmsac
ID: 19581372
doesnt work when you use multi dimensional arrays? I need a solution which will work for both.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 19582807
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 15

Accepted Solution

by:
StingRaY earned 1000 total points
ID: 19659785
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
 

Assisted Solution

by:MageUK
MageUK earned 1000 total points
ID: 19877900
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
 
LVL 16

Expert Comment

by:LeeKowalkowski
ID: 20565479
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
 
LVL 4

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Choosing the right mix of apps is very much necessary for CPAs for making the most of the latest technology through which they can boost their growth.
The article explains the process to deploy a Self-Service password reset portal I developed a few years ago. Hopefully, it will prove useful to someone.  Any comments, bug reports etc. are welcome...
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

572 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