473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

passing arrays between windows

Hi,

I have a HTML page with javascript in it which pops up another HTML page. I
can pass simple variables fairly freely between the two pages. I can pass
objects between them two, and I have noticed that when a method is invoked
on an object, it invokes in the context of the window that created that
object.

Now, when I pass an array from one window to another, the expression:

myArray.constructor == Array

returns false in the second window.

I think I know what's happening - when I reference "Array" in the second
window, I'm talking about the second window's array constructor which is not
the constructor that was used to construct the array.

Unfortunately, this doesn't help me much. What I need is a reliable way of
telling if something is an array or not, because my test "constructor ==
Array" isn't working. Any ideas?

Andy
Jul 20 '05 #1
5 5342
"Andy Fish" <aj****@blueyonder.co.uk> writes:
I have a HTML page with javascript in it which pops up another HTML page. I
can pass simple variables fairly freely between the two pages. I can pass
objects between them two, and I have noticed that when a method is invoked
on an object, it invokes in the context of the window that created that
object.
Yes. All javascript functions are closures, and their free variables refer
to the ones where they were created.
If you write
function blah(){
... window.document...
}
then the "window" variable refers to the one visible where the function
is declared, even if the function is later called by another window.
Now, when I pass an array from one window to another, the expression:

myArray.constructor == Array

returns false in the second window.

I think I know what's happening - when I reference "Array" in the second
window, I'm talking about the second window's array constructor which is not
the constructor that was used to construct the array.
That sounds correct. Functions are objects, and objects have
identities. Functions from two different pages are not the same function.
Unfortunately, this doesn't help me much. What I need is a reliable way of
telling if something is an array or not, because my test "constructor ==
Array" isn't working. Any ideas?


Don't make non-array objects with Array.prototype (or any array) as a
prototype. You never need that, since they won't act as arrays anyway.
If you need any of the Array.prototype methods for your own objects,
assign them manually.

Then, add a clone method to Object.prototype and to Array.prototype,
and whenever cloning an object, call its clone method. The one for
arrays will then make sure that the clone is also an array.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
"Andy Fish" <aj****@blueyonder.co.uk> wrote in message news:<nW*********************@news-text.cableinet.net>...
Hi,

I have a HTML page with javascript in it which pops up another HTML page. I
can pass simple variables fairly freely between the two pages. I can pass
objects between them two, and I have noticed that when a method is invoked
on an object, it invokes in the context of the window that created that
object.

Now, when I pass an array from one window to another, the expression:

myArray.constructor == Array

returns false in the second window.

I think I know what's happening - when I reference "Array" in the second
window, I'm talking about the second window's array constructor which is not
the constructor that was used to construct the array.
Couldn't you test the return value of array_name.constructor.toString() ?

Just a thought.

FWIW. Unfortunately, this doesn't help me much. What I need is a reliable way of
telling if something is an array or not, because my test "constructor ==
Array" isn't working. Any ideas?

Andy

Jul 20 '05 #3

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:1x**********@hotpop.com...

Then, add a clone method to Object.prototype and to Array.prototype,
and whenever cloning an object, call its clone method. The one for
arrays will then make sure that the clone is also an array.


aha - but this is exactly the crux of my problem.

Say window A creates an object containing an array and wants to pass it to
window B. here is a fragment of code from window A:

var userInfo = { id:"1932" name:"John Smith" groups: [ "allstaff",
"accounts" ] } ;
window.opener.updateUserInfo(userInfo);
window.close();

Calling the clone method of the userInfo object will create a new object
owned by window A, whoever calls it. In IE, both the original object and the
clone stop working completely as soon as window A is closed, which is no
good for me.

So it has to be code owned by window B that creates the clone. the problem
is that window B cannot tell whether the groups property is an array or
non-array, so it cannot create a proper clone.

From the discussions we had about 'arrayness' in a different thread, I think
maybe the best approach is to serialize the object into a single string
(using JSON) and then pass this between the windows.

Andy

Jul 20 '05 #4
"Andy Fish" <aj****@blueyonder.co.uk> writes:
aha - but this is exactly the crux of my problem.

Say window A creates an object containing an array and wants to pass it to
window B. here is a fragment of code from window A:

var userInfo = { id:"1932" name:"John Smith" groups: [ "allstaff",
"accounts" ] } ;
window.opener.updateUserInfo(userInfo);
window.close();

Calling the clone method of the userInfo object will create a new object
owned by window A, whoever calls it. In IE, both the original object and the
clone stop working completely as soon as window A is closed, which is no
good for me.
That is bad. It shouldn't stop working just because the window it was
defined in, disappears (but ofcourse, not garbage collecting the window
when it closes will risk keeping it around forever). Alas, if it does,
then we have to work around that.
So it has to be code owned by window B that creates the clone.
Not necessarily. The code might sit in window A, and even be executed
by window A, as long as it uses window B's Array and Object
constructors, right?
the problem is that window B cannot tell whether the groups property
is an array or non-array, so it cannot create a proper clone.
That is why the object/array must have its own clone method. Since code
running from page A (the call to window.opener.updateUserInfo is executed
in the context of window A's global object) and needs to use Window B's
Array and Object constructors, we have to let Window B supply these to
the clone method.

---
function cloneValue(val,toWin) {
if (typeof val == "object" && val !== null) {
return val.clone(toWin)
}
return val; // should we throw error if it is a function?
}

Object.prototype.clone = function (toWin) {
var newObj = new toWin.Object();
for (var i in this) {
newObj[i] = cloneValue(this[i],toWin);
}
return newObj;
}

Array.prototype.clone = function (toWin) {
var newArr = new toWin.Array();
for (var i in this) {
newArr[i] = cloneValue(this[i],toWin);
}
return newArr;
}
---
You can also add clone methods to Date and Regexp prototypes, or even to
Number and String. You will need these clone methods on page B, where
the value to be cloned is created.

You then call this from the updateUserInfo function as:

function updateUserInfo(userInfo) {
...
cloneValue(userInfo,window)
...
}

Due to scope rules, that "window" variable refers to window B's global
object.

Warning: code only tested in IE6.
From the discussions we had about 'arrayness' in a different thread, I think
maybe the best approach is to serialize the object into a single string
(using JSON) and then pass this between the windows.


Never give up! Never surrender!

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
you are truly too clever for your own good ;-))

I hope this code can make it onto a FAQ somewhere

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:br**********@hotpop.com...
"Andy Fish" <aj****@blueyonder.co.uk> writes:
aha - but this is exactly the crux of my problem.

Say window A creates an object containing an array and wants to pass it to window B. here is a fragment of code from window A:

var userInfo = { id:"1932" name:"John Smith" groups: [ "allstaff",
"accounts" ] } ;
window.opener.updateUserInfo(userInfo);
window.close();

Calling the clone method of the userInfo object will create a new object
owned by window A, whoever calls it. In IE, both the original object and the clone stop working completely as soon as window A is closed, which is no
good for me.


That is bad. It shouldn't stop working just because the window it was
defined in, disappears (but ofcourse, not garbage collecting the window
when it closes will risk keeping it around forever). Alas, if it does,
then we have to work around that.
So it has to be code owned by window B that creates the clone.


Not necessarily. The code might sit in window A, and even be executed
by window A, as long as it uses window B's Array and Object
constructors, right?
the problem is that window B cannot tell whether the groups property
is an array or non-array, so it cannot create a proper clone.


That is why the object/array must have its own clone method. Since code
running from page A (the call to window.opener.updateUserInfo is executed
in the context of window A's global object) and needs to use Window B's
Array and Object constructors, we have to let Window B supply these to
the clone method.

---
function cloneValue(val,toWin) {
if (typeof val == "object" && val !== null) {
return val.clone(toWin)
}
return val; // should we throw error if it is a function?
}

Object.prototype.clone = function (toWin) {
var newObj = new toWin.Object();
for (var i in this) {
newObj[i] = cloneValue(this[i],toWin);
}
return newObj;
}

Array.prototype.clone = function (toWin) {
var newArr = new toWin.Array();
for (var i in this) {
newArr[i] = cloneValue(this[i],toWin);
}
return newArr;
}
---
You can also add clone methods to Date and Regexp prototypes, or even to
Number and String. You will need these clone methods on page B, where
the value to be cloned is created.

You then call this from the updateUserInfo function as:

function updateUserInfo(userInfo) {
...
cloneValue(userInfo,window)
...
}

Due to scope rules, that "window" variable refers to window B's global
object.

Warning: code only tested in IE6.
From the discussions we had about 'arrayness' in a different thread, I think maybe the best approach is to serialize the object into a single string
(using JSON) and then pass this between the windows.


Never give up! Never surrender!

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #6

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

Similar topics

12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
58
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...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
2
by: dave.harper | last post by:
I'm relatively new to C++, but have a question regarding functions and arrays. I'm passing a relatively large array to a function several thousand times during the course of a loop, and it seems...
1
by: Kurt Richardson | last post by:
Hi all Sorry to bother you with what is probably a really trivial question for you C++ experts. My programming skill are pretty amateur, but I'm pretty good at VB.NET. However, I'm wanting to...
4
by: dogalacar | last post by:
Hi All, I am trying to pass array of structures from a C dll to C# as msdn sample does(outarrayofstructs sample) but PtrToStructure function gives error : --> "structure must not be a value...
3
by: Mark | last post by:
Hi From what I understand, you can pass arrays from classic ASP to .NET using interop, but you have to change the type of the.NET parameter to object. This seems to be because classic ASP passes...
2
by: goetzie | last post by:
I am using Python 2.4.1 and Numeric 23.8 and running on Windows XP. I am passing a Numeric array of strings (objects) to a C Extension module using the following python code: import Numeric...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
1
by: Fizzics | last post by:
This is my first post here at Bytes. I have been trolling it, mostly with the help of Google searches, for some time now. I have done about all of the searching and reading that I really know how to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...

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.