473,734 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

myArray instanceof Array fails after passing to a different page

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.

By checking "myArray instanceof Array", I have determined that the object
is seen as an array until it is passed to the parent page. Then "myArray
instanceof Array" fails.

Does anybody know why this is? And/Or how I can get around it?

I managed to isolate the problem in the following HTML files.

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

-------------------------
--- parent.htm ---
-------------------------
<HTML>
<script>
function getArray()
{
var ca;
ca = ifra.getArray() ;
alert(ca.join("-"));
alert( "Parent : " + ((ca instanceof Array) ? "Type Array" :
"Unknown
Type"));
}
</script>
<BODY>
<button onclick="getArr ay()">Check child array type</button><br>
<IFRAME id=ifra name=ifra src="C1.htm"></IFRAME>
</BODY>
</HTML>

-------------------------
--- C1.htm ---
-------------------------
<script>
function getArray()
{
var a = new Array( 1,2,3,5,8,13,21 ,34,55,89);
return a;
}

function btn_onclick()
{
var a = getArray();
alert( "Child : " + ((a instanceof Array) ? "Type Array" :
"Unknown
Type"));
}
</script>
<BODY>
<button onclick="btn_on click();">Click for Array</button>
</BODY>
</HTML>


Jul 20 '05 #1
4 13093
> 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.

By checking "myArray instanceof Array", I have determined that the object
is seen as an array until it is passed to the parent page. Then "myArray
instanceof Array" fails.

Does anybody know why this is? And/Or how I can get around it?


When you say 'Array', you are talking about 'window.Array'. 'window' is the
browser's context object, and you get one per page (or frame). All of the arrays
created within a context will have their constructor property set to
'window.Array'.

An array created in a different context has a different window.Array, so your
test

myArray instanceof Array

fails. The ECMAScript standard does not discuss multiple contexts, even though
virtually all implementations support them. The ECMAScript standard also fails
to provide a reliable technique for testing the type of arrays. The obvious
thing would have been

typeof myArray == 'array'

except that unfortunately, it turns out that

typeof myArray == 'object'

which not very useful.

You might try testing for the presence of a well-know array method.

typeof myArray.sort == 'function'

This is not infallible, but it is better than the useless instanceof operator.

www.crockford.com/javascript/javascript.html

Jul 20 '05 #2
John MacIntyre wrote:
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.prototyp e. 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>
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.p rototype in the prototype chain of
myObject". If you executed

function showType()
{
alert("ca instanceof ifra.window.Arr ay: "
+ (ca instanceof ifra.window.Arr ay));
}
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

Jul 20 '05 #3
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

Jul 20 '05 #4
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" <do************ *@senet.andthis .com.au> wrote in message
news:Lv******** ********@nnrp1. ozemail.com.au. ..
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


Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10167
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
22
4639
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last month I had enough of extra proof that the cat doesn't hount mice anymore in more and more situations. And the surrent sicretisme among array and hash is the base for it. I summarized all points in this article:...
5
71016
by: Beginner | last post by:
What is the c++ version of instanceof()? Thank-you
3
3842
by: Andrew Phillipo | last post by:
In IE5 I am working with a lot of code that uses Array.push() as well as for(var i in myArray) {} Great - so I'm fixing the Array.prototype.push function for browsers without it. That much works great. However as soon as I start
8
10718
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
2
4156
by: Neil Munro | last post by:
I'm having some "type" difficulty in passing a .NET array (byref) to a COM based API (Autodesk Inventor). In VB6 the code to return an array of tolerance values is: Dim ToleranceCount As Long Dim ExistingTolerances() As Double 'oSurfBody is declared and assigned in the following Call oSurfBody.GetExistingFacetTolerances(ToleranceCount, ExistingTolerances)
4
1914
by: Sean Inglis | last post by:
Well bizarre to me, anyway. I've distilled it down to two small files: testtop.htm =============================== <html> <head> <script language="Javascript">
7
3412
by: arnuld | last post by:
this programme gives unusual output. i am not able to find out where the semantic bug lies: /* C++ Primer - 4/e * * an example from section section 7.2.4, page 241 * STATEMENT * write a function that prints the elements of an array. don't use pointer to an array as parameter because pointer will be copied, use reference to the array instead.
1
7110
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down so if you want to start by reading that, search for +++ Also note that I am unable to reproduce...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.