Solved

Why does an array lose it's type when referenced from another window?

Posted on 2001-08-02
24
279 Views
Last Modified: 2012-06-21
I have done some experimenting, and I have found out, that there's a strange problem with types in Internet Explorer 5.5 (possibly other versions as well).

Example scenario :

window1:
var a1 = new Array("a", "c", "b");
var b1 = a1;

window2:
var a2 = window1.a1;

When I create an array (a1) in window1 and put data in it, and then create a reference to it (b1), then I can sort the array no problem, using either the original array (a1.sort(mySortFunc)), or the reference var (b1.sort(mySortFunc)).

Now, if I create a reference to this array from another window/frame (window2), and try to use sort() on that reference (a2.sort(mySortFunc)), then I get a runtime error of the type "JScript object expected", meaning that it thinks the reference a2 is not an array anymore, but my own type.
The funny thing is, that I can use the no-argument sort() with no error - it just doesn't have any function.
But if I try to use my own sorting function on the a2 reference, I get the runtime error.

I have tested this VERY thoroughly, and I am absolutely certain that is exactly the fact that it looses it's type that's the problem. It's not a problem with my sorting function or the adressing of the other window, so I don't need suggestions in that direction.

My question is this simple :
Is there any way to get a reference to a1 in window2 that still has retained it's type, so I can use the sort(myfunc) method on the reference, without runtime errors, and with success. If not : Why? is it a bug or a security limitation?

If I am unclear, then feel free to ask me as much as needed.

Thank you.
Brian Meidell
Software Engineer
0
Comment
Question by:setien
  • 12
  • 6
  • 4
  • +1
24 Comments
 

Author Comment

by:setien
ID: 6345846
I am aware that the code in the example is not kosher (problem with the window-window addressing in the line with a2 - but it's just to illustrate what I mean.
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6346387
>>It's not a problem with my sorting function or the adressing of the other
window, so I don't need suggestions in that direction.


As a software Dev, you should never be that sure of anything:
okay, you are doing it not quite right....

in a frameset

with two frames pages, named window1 and window2

in frame page window1:
<SCRIPT>
a=new Array('one','two','three');
</SCRIPT>


in framepage window2:
<SCRIPT>
function al()
{
a=new Array()
a=top.left.ar
alert(a[2])
}
window.onload=al;
</SCRIPT>


so as you can see it certainly does work.
and for added emphasis, it is posted here

http://www.milestonemortgage.com/html/frametest.htm

which you can view the source of to make sure it is really working.


Regards,

Bob

PS: if you are using a popup window, here is an example.

<SCRIPT>
ar=new Array("one","two","three","four")
d=window.open();
d.document.write('<SCRIPT>\na=new Array();\na=opener.ar\nalert(a[2])\n</SCRI' + 'PT>')
</SCRIPT>
or in other words, you need to use opener.

0
 
LVL 14

Expert Comment

by:avner
ID: 6346394
setien,

The only thing I can suggest is to avoid any problems , just clone the variable , and then sort it then overwrite the other variable.

window2.aa=window1.aa;

window2.aa.sort(moshe);
window1.aa=window2.aa;


That's the simple suggestion , But..
I hardly beleive "an array lose it's type " , and I guess it's something you missed during the way (probabaly in the sorting function).
0
 

Author Comment

by:setien
ID: 6346485
To bebonham :

I didn't express myself properly, I think - you missed the point. There's none of what you demonstrate that I haven't done without a problem.
The point is : Try using a custom algorithm with Array.sort(func) on your array in the second (right) frame.
It will give you an error, I am sure. If it doesn't - try doing it from different windows instead of a different frame (Yes, I am aware that it's supposed to be the same, but you never know).

Thanks for your time and effort, but it didn't answer my question, I am sorry.
0
 

Author Comment

by:setien
ID: 6346501

To avner : I can't clone the variable. The problem is a simplification of a part of a rather complex site, and the objects are not a simple collection of a few properties, but rather several hundred in many layers.

The problem with cloning it is, that it's too timeconsuming, and that I actually need a reference to the original array, otherwise the first Array would loose it's ability to do a custom sort when I copy the clone back on top of the original array.
I am afraid cloning it is just not an option, and it  doesn't work as intended anyway. I already tried it.
0
 

Author Comment

by:setien
ID: 6346524
To everyone :

I upped the points - it's all the points I have.

I must stress that the problem occurs when a reference to an array in a different window/frame has it's sort(customSortFunction) method invoked.

And Avner - my sort function is very simple, and I have also tried to replace it with a surefire one - still the same problem:

Internet Explorer gives you a "JScript object expected" error, which means that it doesn't think it's appropriate to call the sort(customSortFunction) method on it, because it is no longer an array.

I am accessing an array that is referenced again and again in many layers (var a = new Array(); var b = a; var c = b; and so on...), and the place where is looses the ability to sort is once it crosses the frame-frame "boundary".

Try doing this :

frame1:

var a = new Array("a", "b", "d", "c");
a.sort(myFunc);
var b = a;
b.sort(myFunc);
var c = b;
c.sort(myFunc);

// none of these sorts have any problem sorting

frame2:
var a2 = window.top.frame1.document.c;
c.sort(myFunc);

// end

Of course I haven't written out myFunc here, but it doesn't really matter. Also, I know it needs to be defined in both frames.
The sorts perform flawlessly through the reference layers a, b and c but as soon as it goes to a2 in the other frame, it suddenly gives you an error telling you that it isn't a JScript object (meaning that it's not a real Array() anymore).


0
 
LVL 8

Expert Comment

by:bebonham
ID: 6346672
nope, the sort function with an compare func argument works too in version 5.5

http://www.milestonemortgage.com/html/frametest.htm

check it out with a numerical sort function.


I am guessing the problem is in your sort function.

regardless...

if you check the link that should solve your problem.

Bob

ps....

this is the code for the other frame with the sort func.:

<SCRIPT>
function al()
{
a=new Array()
a=top.left.ar
a.sort(cmpf)


for(i=0;i<a.length;i++)
{
alert(a[i])
}

}

function cmpf(a,b)
{
return a-b;
}
window.onload=al;

</SCRIPT>
0
 

Author Comment

by:setien
ID: 6347599
To Bebonham and everyone else:
It is not the sort function. It's identical to yours, for the debugging phase being.

I just modified your example so it looked more like mine, because you were waiting for an onload, which I can't do, since I need to document.write the sorted data before the document is loaded. So I changed it, and curiously it STILL worked. It's very strange. The only conclusion that I could draw was, that it's because it is between actual windows, rather than frames.

So I changed the example to spawn a window instead - just as I wrote in my first reply to you.
The result can be seen at :
http://www.mindflow.dk/get/arrays/index.html
As you can see, it's your code, your sort routine, and my excact problem with the "JScript object expected" error.

While I was writing this, I just tested the address above in Netscape 6, and there is no problem.
I looks like it might be a browser bug in IE - so my question is still : Is it possible to get around it?

If I had anymore points, I'd up the stakes.

Kind regards,
Brian Meidell Andersen
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 6347629
You need a way to identify the function. Either place the MySortFunc in both window 1 and 2 or save the function somewhere in memory. Like:
navigator.myfunc = MySortFunc;

Now the navigator.myfunc function call will ONLY work if the function is still loaded in memory.

Regards,
CJ
0
 

Author Comment

by:setien
ID: 6347664
CJ_S :
That doesn't work. I modified the custom sort function, so it's defined in the in the frameset instead, like this :

navigator.cmpf = function(a,b) {
return a-b;
}

And then used navigator.cmpf as the custom sort function - which resulted in the same as the last few attempts : It'll work across frames, but not across windows.

And still: only in IE, no problem in Netscape 6.
0
 
LVL 14

Expert Comment

by:avner
ID: 6348495
have you tried , for the sake of the test to duplicate the method to both windows ?
0
 

Author Comment

by:setien
ID: 6348505
avner : Yes. I try all the suggestions that are posted in here, if I haven't already tried them.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 22

Expert Comment

by:CJ_S
ID: 6348533
Have you tried:

var a2 = new Array()
a2 = window1.a1;
0
 
LVL 14

Expert Comment

by:avner
ID: 6348614
setien ,
Like you I was playing with this for a while.
It seems like an Internal IE bug in referencing the external sort method.

I tried this :
a.sort(new Function("return 0"))

and still it returned the same error.

I would say something like this :
"When trying to sort array that is not from the current window with a custom sort method , IE fails. "

Why or How to solve it is something that beyond us (since we are not developing IE) I would suggest you'll work on a different approach.

Sorry I couldn't help more.
0
 

Author Comment

by:setien
ID: 6348694
avner :
Thanks for your time and effort though. I already know that it was a problem in explorer, since I saw it worked in netscape. I am working on alternative methods, and the questions stand :

How do I work around this bug?
0
 
LVL 8

Accepted Solution

by:
bebonham earned 75 total points
ID: 6348708
okay, I think a few of us were starting to catch on to this...

it is a problem with reference to the sort function...


so check this out now and tell me what you think of it:

http://www.milestonemortgage.com/html/frametest.htm

0
 
LVL 14

Expert Comment

by:avner
ID: 6348717
:)
Looks like it.
0
 
LVL 14

Expert Comment

by:avner
ID: 6348775
setien,
Why did you give it B grade ?
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6348799
oh well, that's fine...

he wanted it to work right, and it is still a hack ( a little),

but I certianly appreciate you looking out for me, avner!


Bob
0
 

Author Comment

by:setien
ID: 6348801
It wasn't an ideal answer. I am sorry if that's an insult - I am totally new to this site. It's my first question.
I did not intend it to be bad. I thought "good" was a good thing :)

The solution allows a sort to be invoked from another frame, but the function still has to be placed in the first frame, so it only partially solves the problem I am sitting here with, since it does not give me the flexibility to sort the array in window1 with functions I make on the fly from window2.

Bebonham, I did not mean to make you look bad - It was a good answer.
0
 

Author Comment

by:setien
ID: 6348809
Great - thanks for understanding Bebonham. It's excactly right what you are saying - it's still a hack.

I am still very grateful, as I am one step closer to a solution.
0
 
LVL 14

Expert Comment

by:avner
ID: 6348828
setien ,
No need for apologies .
Just for the future , In EE - atleast that's my philosophy - the Grade should reflect the affort that was put in the question with a combination of the question's level.

So a Question that was answerd 100% correct should be A, but also a question that was answerd "Cannot be done" (That's when people ask 'How to Make the client's Monitor jump around his house ?')  Should also be A.

I guess we should also thank you , it was an intresting problem.

Regards ,

      Avner.
0
 

Author Comment

by:setien
ID: 6348861
Thank you, avner. I see the logic in that.
The effort was great, and the quality of solution was quite decent, so I if that's the EE way, I stand corrected, and if there's a way to correct it to an A, then I will fix it.

If not, you can always refer to this posting, Bebonham :)

I'm glad you liked the problem, and I am glad I have found a place like this, where people are at your own level of expertise.

This is great. Thanks, you guys.
0
 

Author Comment

by:setien
ID: 6375442
Epilogue :

I ended up figuring out, that the problem is actually that the sort function looses it's type, when referring it from another window - it turns from type "function" to type "object".

A workaround that works, is to make a function in the same window as the array, that takes an object, and uses that as the sort function. This way you can still use functions created in window 2 to sort the array in array 1.

Here's an example :
Window 1 :
var a = new Array(1,2,3,11,6,123);
function sortFunctionFromObject(obj) {
   var f = function(a,b) {
      return obj(a,b);
   }
}

Window2:
function mySort(a,b) {...}
window1.a.sort(window1.sortFunctionFromObject(mySort));

And voila, it works :)

Hope this helps someone else out there.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now