views:

473

answers:

3

I have an iframe in a form. The iframe contains some info that I want deliver to the form parent by instance of Array. The problem: the Array instance loses it's type and becomes an object! The iframe function:

function getIDS2() { return new Array(); }

The parent call code:

alert(top.frames["sup_search"].getIDS2() instanceof Array);

Of course the answer for the alert is false... So, I can fix it by doing this:

var arr = [];
for(var i =0; i < SuppliersIDs.length; i+=1) {
    arr.push(SuppliersIDs[i]);
}

Where SuppliersIDs is the delivered array and arr is the new true type array. But why is this not working as I want it to? By the way, is there any there a way to access the iframe func with jQuery??

Thanks for help.

+7  A: 

Because each page has a global context with its own "Array" function, if code on one page passes an array to a function on a separate page, the test "array instanceof Array" will fail. For Array you can do this instead:

var arr = top.frames["sup_search"].getIDS2();
var isArray = arr && Object.prototype.toString.call(arr)=="[object Array]";

It feels hacky but it works.

Darin Dimitrov
It might feel hacky, but ECMA-262 defines the behaviour of `Object.prototype.toString()`, so it's guaranteed to work in conforming implementations
Christoph
A: 

@darin thanks for the answer. You definitely found the problem Actualy I want do some casting. I need to get it as an Array like the source. I wrote the code like this:

var arr = new Array(SuppliersIDs);

The result is an array of objects not an array of integers as the original.

ari
A: 

I found way to access iframe function with jquery

$(this).contents()[0].defaultView.yourFunc()
ari