Sean Inglis wrote:
> A few deprecated features aside, when I load testtop.htm, instanceof
> array is true as I'd expect, but when I check this from the button in
> testchild.htm, instanceof array is false.
When the frameset document resolves "Array" it finds it defined in its
window object: window.Array.
The main document has its own window object, and its own version of
window.Array.
So, the main documents Array object isn't the same object as the frameset's
Array object, even though they are both instances of the same native object.
Instead of this:
> alert((parent.TOPTEST) instanceof Array);
try this:
alert((parent.TOPTEST) instanceof parent.Array);
Or, if you just care that the object looks and acts like an Array, try
something like:
/**
* Return true if an object is not undefined
*/
function def(o) {
return typeof(o)!="undefined";
}
/**
* Try to figure out if an object can be treated like an Array - ie,
iterated over using numeric indexes
*/
function isArrayLike(o) {
if (o==null || typeof(o)!="object" || typeof(o.length)!="number") {
return false;
}
// Check to see if the object is an instance of the window's Array object
if (def(Array) && def(o.constructor) && o.constructor==Array) {
return true;
}
// It might be an array defined from another window object - check to see
if it has an Array's methods
if (typeof(o.join)=="function" && typeof(o.sort)=="function" &&
typeof(o.reverse)=="function") {
return true;
}
// As a last resort, let's see if index [0] is defined
return (o.length==0 || def(o[0]));
};
--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com