JavaScript: when I pass array from iframe function the array lose his type!

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.


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 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.


我找到了使用jquery访问iframe函数的方法

$(this).contents()[0].defaultView.yourFunc()
链接地址: http://www.djcxy.com/p/4746.html

上一篇: 有没有办法压缩一些非提交的提交

下一篇: JavaScript:当我从iframe函数传递数组时,数组会丢失他的类型!