Cannot sort an array of arrays in window.opener using javascript

from my first window (I will call it as window 1) I open the second window (window 2) using

var w = window.open("", ""); 

From window 2, I reference and manipulate the array defined in window 1 using code like

  window.opener.object2[0].drivedistance = driveDistance; 

This code is working fine. However, before displaying the results of this array in window 2, I need to sort it. My sorting attempt:

  window.opener.objects2.sort(compareFunc) function compareFunc(a, b){ return (a.drivedistance - b.drivedistance) } 

If I use the same method in jsfiddle without calling from a remote window, it works fine.

http://jsfiddle.net/98Q4D/10/

When I look at the code, I see that it is executing the code in compareDriving. No exceptions are thrown. However, after this is done, call compareDriving, the array is not sorted. I don’t know, maybe this is some kind of security problem, because the array that I am sorting exists in window 1, but my javascript is in window 2? Any ideas on why this sorting code doesn't work would be greatly appreciated.

Based on the comments, I put together a jsfiddle problem that seems to demonstrate it well. To understand the final solution, I need page 1 to be the collection repository, but I need page 2 to initialize the logic, which causes the assembly on page 1 to sort.

http://jsfiddle.net/mexP6/26/

Bizarre After playing with this a little more, it looks like I can do what I want by passing the sort function to the handler that exists on page 1, and not on page 2. But when I run the function remotely on page 2, it is sorted in reverse order as when I start it from page 1. I can probably play around with this to get it working, but I will still appreciate any suggestions for best practices. Here is my last scenario of my findings.

http://jsfiddle.net/mexP6/23/

Keep in mind that you need to enable pop-ups to check them when I work with pop-up functions.

Change I find that cross-browser results are very spotty on the latest jsfiddle. My buddy tried it in chrome and it worked for him. I tried this in my chrome and the results are not sorted. IE is sorted in reverse order. I don't have firefox to work at all. Any help on cross-window sorting method compatible with cross-browser is what I think I'm really here. Thanks for any answers.

+4
source share
1 answer

I think that you are faced with rules about what script actions are allowed between individual windows, which will obviously differ between browsers. If you look at your initial example (below), you are executing a method (sorting) of an object (array) that is in the parent window, but you pass it a function from the popup.

 window.opener.objects2.sort(compareFunc) function compareFunc(a, b){ return (a.drivedistance - b.drivedistance) } 

I usually find it better to allow the parent window to handle any changes to its own data. This is essentially what you tried to do in your last jsfiddle attempt ( http://jsfiddle.net/mexP6/23/ ), but it was thrown by a spelling error in the name of the sort function. You wrote "compareFuncFromMainPag" instead of "compareFuncFromMainPage". This would have worked differently because you requested a sorting method to sort the array using the compare function, which also applies to the parent window.

Personally, I would make it even more explicit, only the calling functions in the parent window in the popup. These functions in the parent window will perform all data manipulations. Consider this modification of your last jsfiddle as an example of what I suggest:

http://jsfiddle.net/jarbirdy/dUMMw/8/

+1
source

All Articles