JavaScript:当我从iframe函数传递数组时,数组会失去他的类型! [英] JavaScript: when I pass array from iframe function the array lose his type!

查看:207
本文介绍了JavaScript:当我从iframe函数传递数组时,数组会失去他的类型!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单中有一个iframe。
iframe包含一些我希望通过Array实例传递给父表单的信息。
问题:Array实例丢失了它的类型并成为一个对象!
iframe函数:

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

父母代码:

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

其中,SuppliersIDs是交付的数组,arr是新的真实类型数组。
但为什么这不符合我的要求呢?
顺便问一下,有没有办法用jQuery访问iframe函数?

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

感谢您的帮助。

推荐答案

因为每个页面都有一个带有自己Array函数的全局上下文,如果一个页面上的代码将一个数组传递给一个单独页面上的函数,那么该测试array instanceof Array将失败。对于数组,你可以这样做:

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]";

感觉hacky但是有效。

It feels hacky but it works.

这篇关于JavaScript:当我从iframe函数传递数组时,数组会失去他的类型!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆