HighDots Forums  

Re: myArray instanceof Array fails after passing to a different page

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: myArray instanceof Array fails after passing to a different page in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dom Leonard
 
Posts: n/a

Default Re: myArray instanceof Array fails after passing to a different page - 09-24-2003 , 02:49 AM






John MacIntyre wrote:
Quote:
Hi,

I have a page with a series of child pages loaded into an iframe. When I
move from page to page, I store an object containing the child's control
data in a variable on the main page, then use that data to populate the
controls when the child page is opened again.

One of these objects contains an Array, and the page reloads fine using
myArray[0], myArray[1], etc... But when I try perform some array methods
on it (i.e.slice) ... it does not recognize it as an array.

You will be familiar with the fact that page javascript values are
destroyed by closing a window, page reload, document.write after
window.onload or simply navigating to a new page.

Less well known, but corroborated by testing, is that the *value* of at
least some of the global properties supplied by the scripting engine are
also updated as a result of navigation within a window. In particular,
the value of window.Array seen within the iframe is not constant across
page navigation.

What your program does is to execute code defined within the iframe from
a thread originating in the parent. For IE at least, the empirical
result is that the Array object returned is created in object data space
of the parent and is not destroyed by changing URLs within the iframe.

However, array methods are inherited from Array.prototype, and the array
returned from iframe code is prototyped on
<iframe>.window.Array.prototype. This object *is* being destroyed by
navigation within the iframe.

In effect the array stored in the parent has lost its prototype object
[1]. Local properties of the array can still be accessed as you have
already found.

The suggested workaround is to copy non-object values from the iframe to
an array created by parent frame code, in a parent frame execution
thread. (Or "experiment, experiment, experiment!"

<snip>

Quote:
function getArray()
{
var ca;
ca = ifra.getArray();
alert(ca.join("-"));
alert( "Parent : " + ((ca instanceof Array) ? "Type Array" : "Unknown Type"));
}
[1] "myObject instanceof myConstructor" answers the question "is the
current value of myConstructor.prototype in the prototype chain of
myObject". If you executed

function showType()
{
alert("ca instanceof ifra.window.Array: "
+ (ca instanceof ifra.window.Array));
}
you would see true immediately after calling getArray(), but false if
the iframe document is replaced in the mean time.

BTW, this response is not intended to replace Douglas Crawford's
execellent reply, but simply to investigate different aspects and look
at some snipped code.

HTH,

Dom



Reply With Quote
  #2  
Old   
Dom Leonard
 
Posts: n/a

Default Re: myArray instanceof Array fails after passing to a different page - 09-24-2003 , 03:21 AM






Dom Leonard wrote:

Quote:
BTW, this response is not intended to replace Douglas
Crockford's
execellent reply,
Sorry Douglas, I primarily file by first name in my head and didn't spot
misquoting your last name until after posting. My sincere appologies.

Dom







Reply With Quote
  #3  
Old   
John MacIntyre
 
Posts: n/a

Default Re: myArray instanceof Array fails after passing to a different page - 09-24-2003 , 05:07 PM



Thanks, guys for the great replies. These were among the best explanations
I've ever gotten from a newsgroup.

For the record, the way I got around this is to create a function in the
parent page which I called from the child page. This way when it was passed
back to the parent ... it was recognized. Not the cleanest way .. but it
was effective.

Thanks again,
--
John MacIntyre
VC++ / VB / ASP / Database Developer
http://www.johnmacintyre.ca


"Dom Leonard" <doml.removethis (AT) senet (DOT) andthis.com.au> wrote

Quote:
Dom Leonard wrote:

BTW, this response is not intended to replace Douglas
Crockford's
execellent reply,

Sorry Douglas, I primarily file by first name in my head and didn't spot
misquoting your last name until after posting. My sincere appologies.

Dom








Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.