array passed from one frame to another gets corrupted when the first frame is reloaded.

Discussion in 'Javascript' started by Eyal, Sep 10, 2007.

  1. Eyal

    Eyal Guest

    hi all,

    i have researched the following problem and found no solution so i'm
    turning to u guys/girls for help.
    i am aware that my research could have been more complete but i have a
    dead-line to meet and i did put a few good hours into it to no avail.

    to the problem:
    frame1 creates an array called array1
    it then passes this array to frame2 by using: top.frame2["array1ref"]=
    array1;
    and then goes on to working with array1ref as needed.
    this is all good and dandy until i reload frame1.
    when reloading frame1 in firefox frame1 still has full access to
    array1ref.
    when doing the same in internet explorer 7 frame1 has only limited
    access to array1ref.
    specifically: it can read the array and use its elements but calling
    any member function of the array results in the following error
    message when debugging with visual studio 2003: "Unable to evaluate
    the expression. Catastrophic failure".

    any help will be greatly appreciated,
    eyal.
     
    Eyal, Sep 10, 2007
    #1
    1. Advertising

  2. Eyal

    Erwin Moller Guest

    Re: array passed from one frame to another gets corrupted when thefirst frame is reloaded.

    Eyal wrote:
    > hi all,
    >
    > i have researched the following problem and found no solution so i'm
    > turning to u guys/girls for help.
    > i am aware that my research could have been more complete but i have a
    > dead-line to meet and i did put a few good hours into it to no avail.
    >
    > to the problem:
    > frame1 creates an array called array1
    > it then passes this array to frame2 by using: top.frame2["array1ref"]=
    > array1;
    > and then goes on to working with array1ref as needed.
    > this is all good and dandy until i reload frame1.
    > when reloading frame1 in firefox frame1 still has full access to
    > array1ref.
    > when doing the same in internet explorer 7 frame1 has only limited
    > access to array1ref.
    > specifically: it can read the array and use its elements but calling
    > any member function of the array results in the following error
    > message when debugging with visual studio 2003: "Unable to evaluate
    > the expression. Catastrophic failure".
    >
    > any help will be greatly appreciated,
    > eyal.
    >



    Hi,

    You created a somewhat strange situation.

    Consider by-value and by-reference:

    - Javascript passes around by-value and by-reference, depending on what
    you are passing. Simple vars, like numbers and booleans, are passed
    by-value.
    Complex vars by reference: eg Arrays and Objects (Arrays ARE objects,
    but leave that for now.)
    And Strings are immutable, and for all practical purposes you don't have
    to know HOW thay are passed in JavaScript (might even be implementation
    dependant.)

    Here is a little simple JS that demonstrates some by-valu and some
    by-ref behaviour.

    <html>
    <head><title>test</title></head>
    <body>

    <script type="text/javascript">

    function myFunc(whatAmI){
    whatAmI = 10;
    }

    var simpleVar = 22;
    myFunc(simpleVar);
    alert ("example1: "+simpleVar);

    // This will alert 22, since myFunct received the value by-value, and
    not by-reference.


    // Example 2: Passing by reference
    function myFunc2(whatAmI){
    whatAmI[0] = "test";
    }

    var complexVar = new Array(1,2,3,4);
    myFunc2(complexVar);
    alert ("example2: "+complexVar);

    // This will alert test,2,3,4


    // Example3:
    // Actually things are a bit more complicated.
    // If a function receives a variable by reference, it receives a copy of
    the reference
    // so overwriting the whole object fails outside the function. See in
    this example
    function myFunc3(whatAmI){
    var useMe = new Array(100,200,300);
    whatAmI = useMe;
    }
    var complexVar2 = new Array(-1,-2,-3);
    myFunc3(complexVar2);
    alert(complexVar2);


    </script>

    </body>
    </html>


    To your question:
    Window2 holds a reference to a datastructure (array) that is in window1.
    You throw away window1.

    Now you created the situation that the browser must decide what to do
    with the reference hold by a js-var in window2.
    Whatever it holds (if any), it will NOT be the new one in window1.

    You also must consider the 'mark-and-sweep' garbage collection strategy
    build in JS engines.
    This garbage collection mechanism is responsibly for detecting that
    nothing in an environment holds a reference to a certain structure, and
    if so, it can free the memory allocated to this structure, since nothing
    in the language can ever reach it anymore.
    (This is done by simply counting the references to a certain
    memorystructure).

    The best way out is avoiding this situation:
    I would do it as follows:
    - In window 2 NEVER store the reference, but simple address it again in
    window1 when needed.
    - Whenever window1 refreshes, make sure window2 is told to refresh
    whatever it was it was doing, based on the new value of the reference.
    (Probably use an onLoad event handler for this in window1)

    Hope this helps explaining why you see such weird behaviour.

    Regards,
    Erwin Moller
     
    Erwin Moller, Sep 10, 2007
    #2
    1. Advertising

  3. Eyal

    Eyal Guest

    On Sep 10, 12:37 pm, Erwin Moller
    <> wrote:
    > Eyal wrote:
    > > hi all,

    >
    > > i have researched the following problem and found no solution so i'm
    > > turning to u guys/girls for help.
    > > i am aware that my research could have been more complete but i have a
    > > dead-line to meet and i did put a few good hours into it to no avail.

    >
    > > to the problem:
    > > frame1 creates an array called array1
    > > it then passes this array to frame2 by using: top.frame2["array1ref"]=
    > > array1;
    > > and then goes on to working with array1ref as needed.
    > > this is all good and dandy until i reload frame1.
    > > when reloading frame1 in firefox frame1 still has full access to
    > > array1ref.
    > > when doing the same in internet explorer 7 frame1 has only limited
    > > access to array1ref.
    > > specifically: it can read the array and use its elements but calling
    > > any member function of the array results in the following error
    > > message when debugging with visual studio 2003: "Unable to evaluate
    > > the expression. Catastrophic failure".

    >
    > > any help will be greatly appreciated,
    > > eyal.

    >
    > Hi,
    >
    > You created a somewhat strange situation.
    >
    > Consider by-value and by-reference:
    >
    > - Javascript passes around by-value and by-reference, depending on what
    > you are passing. Simple vars, like numbers and booleans, are passed
    > by-value.
    > Complex vars by reference: eg Arrays and Objects (Arrays ARE objects,
    > but leave that for now.)
    > And Strings are immutable, and for all practical purposes you don't have
    > to know HOW thay are passed in JavaScript (might even be implementation
    > dependant.)
    >
    > Here is a little simple JS that demonstrates some by-valu and some
    > by-ref behaviour.
    >
    > <html>
    > <head><title>test</title></head>
    > <body>
    >
    > <script type="text/javascript">
    >
    > function myFunc(whatAmI){
    > whatAmI = 10;
    >
    > }
    >
    > var simpleVar = 22;
    > myFunc(simpleVar);
    > alert ("example1: "+simpleVar);
    >
    > // This will alert 22, since myFunct received the value by-value, and
    > not by-reference.
    >
    > // Example 2: Passing by reference
    > function myFunc2(whatAmI){
    > whatAmI[0] = "test";
    >
    > }
    >
    > var complexVar = new Array(1,2,3,4);
    > myFunc2(complexVar);
    > alert ("example2: "+complexVar);
    >
    > // This will alert test,2,3,4
    >
    > // Example3:
    > // Actually things are a bit more complicated.
    > // If a function receives a variable by reference, it receives a copy of
    > the reference
    > // so overwriting the whole object fails outside the function. See in
    > this example
    > function myFunc3(whatAmI){
    > var useMe = new Array(100,200,300);
    > whatAmI = useMe;}
    >
    > var complexVar2 = new Array(-1,-2,-3);
    > myFunc3(complexVar2);
    > alert(complexVar2);
    >
    > </script>
    >
    > </body>
    > </html>
    >
    > To your question:
    > Window2 holds a reference to a datastructure (array) that is in window1.
    > You throw away window1.
    >
    > Now you created the situation that the browser must decide what to do
    > with the reference hold by a js-var in window2.
    > Whatever it holds (if any), it will NOT be the new one in window1.
    >
    > You also must consider the 'mark-and-sweep' garbage collection strategy
    > build in JS engines.
    > This garbage collection mechanism is responsibly for detecting that
    > nothing in an environment holds a reference to a certain structure, and
    > if so, it can free the memory allocated to this structure, since nothing
    > in the language can ever reach it anymore.
    > (This is done by simply counting the references to a certain
    > memorystructure).
    >
    > The best way out is avoiding this situation:
    > I would do it as follows:
    > - In window 2 NEVER store the reference, but simple address it again in
    > window1 when needed.
    > - Whenever window1 refreshes, make sure window2 is told to refresh
    > whatever it was it was doing, based on the new value of the reference.
    > (Probably use an onLoad event handler for this in window1)
    >
    > Hope this helps explaining why you see such weird behaviour.
    >
    > Regards,
    > Erwin Moller- Hide quoted text -
    >
    > - Show quoted text -


    hi Erwin,

    thanks alot for all the details.
    i understand the concepts of by-val and by-ref and was guessing along
    the lines of your explanation (though the part on the inner workings
    of the script engine is new to me and was a great lead).
    sadly the solution part that u offer in the end is not relevant in my
    case since window2's sole purpose is to cache vars so that they are
    present even if window1 is refreshed or even loads a totally new page.
    i was aiming at achieving a cookies' effect san the cookies.
    so actually i could have rephrased my problem to:
    "please help me find a replacement for cookies that gives me client
    side caching."
    the only other relatively simple way i can think of is passing the
    cached values to every page loaded as url params.

    any thoughts about this new data?

    thanks alot,
    eyal.
     
    Eyal, Sep 10, 2007
    #3
  4. Eyal

    Erwin Moller Guest

    Re: array passed from one frame to another gets corrupted when thefirst frame is reloaded.

    Eyal wrote:
    > On Sep 10, 12:37 pm, Erwin Moller
    > <> wrote:
    >> Eyal wrote:
    >>> hi all,
    >>> i have researched the following problem and found no solution so i'm
    >>> turning to u guys/girls for help.
    >>> i am aware that my research could have been more complete but i have a
    >>> dead-line to meet and i did put a few good hours into it to no avail.
    >>> to the problem:
    >>> frame1 creates an array called array1
    >>> it then passes this array to frame2 by using: top.frame2["array1ref"]=
    >>> array1;
    >>> and then goes on to working with array1ref as needed.
    >>> this is all good and dandy until i reload frame1.
    >>> when reloading frame1 in firefox frame1 still has full access to
    >>> array1ref.
    >>> when doing the same in internet explorer 7 frame1 has only limited
    >>> access to array1ref.
    >>> specifically: it can read the array and use its elements but calling
    >>> any member function of the array results in the following error
    >>> message when debugging with visual studio 2003: "Unable to evaluate
    >>> the expression. Catastrophic failure".
    >>> any help will be greatly appreciated,
    >>> eyal.

    >> Hi,
    >>
    >> You created a somewhat strange situation.
    >>
    >> Consider by-value and by-reference:
    >>
    >> - Javascript passes around by-value and by-reference, depending on what
    >> you are passing. Simple vars, like numbers and booleans, are passed
    >> by-value.
    >> Complex vars by reference: eg Arrays and Objects (Arrays ARE objects,
    >> but leave that for now.)
    >> And Strings are immutable, and for all practical purposes you don't have
    >> to know HOW thay are passed in JavaScript (might even be implementation
    >> dependant.)
    >>
    >> Here is a little simple JS that demonstrates some by-valu and some
    >> by-ref behaviour.
    >>
    >> <html>
    >> <head><title>test</title></head>
    >> <body>
    >>
    >> <script type="text/javascript">
    >>
    >> function myFunc(whatAmI){
    >> whatAmI = 10;
    >>
    >> }
    >>
    >> var simpleVar = 22;
    >> myFunc(simpleVar);
    >> alert ("example1: "+simpleVar);
    >>
    >> // This will alert 22, since myFunct received the value by-value, and
    >> not by-reference.
    >>
    >> // Example 2: Passing by reference
    >> function myFunc2(whatAmI){
    >> whatAmI[0] = "test";
    >>
    >> }
    >>
    >> var complexVar = new Array(1,2,3,4);
    >> myFunc2(complexVar);
    >> alert ("example2: "+complexVar);
    >>
    >> // This will alert test,2,3,4
    >>
    >> // Example3:
    >> // Actually things are a bit more complicated.
    >> // If a function receives a variable by reference, it receives a copy of
    >> the reference
    >> // so overwriting the whole object fails outside the function. See in
    >> this example
    >> function myFunc3(whatAmI){
    >> var useMe = new Array(100,200,300);
    >> whatAmI = useMe;}
    >>
    >> var complexVar2 = new Array(-1,-2,-3);
    >> myFunc3(complexVar2);
    >> alert(complexVar2);
    >>
    >> </script>
    >>
    >> </body>
    >> </html>
    >>
    >> To your question:
    >> Window2 holds a reference to a datastructure (array) that is in window1.
    >> You throw away window1.
    >>
    >> Now you created the situation that the browser must decide what to do
    >> with the reference hold by a js-var in window2.
    >> Whatever it holds (if any), it will NOT be the new one in window1.
    >>
    >> You also must consider the 'mark-and-sweep' garbage collection strategy
    >> build in JS engines.
    >> This garbage collection mechanism is responsibly for detecting that
    >> nothing in an environment holds a reference to a certain structure, and
    >> if so, it can free the memory allocated to this structure, since nothing
    >> in the language can ever reach it anymore.
    >> (This is done by simply counting the references to a certain
    >> memorystructure).
    >>
    >> The best way out is avoiding this situation:
    >> I would do it as follows:
    >> - In window 2 NEVER store the reference, but simple address it again in
    >> window1 when needed.
    >> - Whenever window1 refreshes, make sure window2 is told to refresh
    >> whatever it was it was doing, based on the new value of the reference.
    >> (Probably use an onLoad event handler for this in window1)
    >>
    >> Hope this helps explaining why you see such weird behaviour.
    >>
    >> Regards,
    >> Erwin Moller- Hide quoted text -
    >>
    >> - Show quoted text -

    >
    > hi Erwin,
    >
    > thanks alot for all the details.
    > i understand the concepts of by-val and by-ref and was guessing along
    > the lines of your explanation (though the part on the inner workings
    > of the script engine is new to me and was a great lead).
    > sadly the solution part that u offer in the end is not relevant in my
    > case since window2's sole purpose is to cache vars so that they are
    > present even if window1 is refreshed or even loads a totally new page.
    > i was aiming at achieving a cookies' effect san the cookies.
    > so actually i could have rephrased my problem to:
    > "please help me find a replacement for cookies that gives me client
    > side caching."


    Aha, that makes it a lot clearer. :)

    > the only other relatively simple way i can think of is passing the
    > cached values to every page loaded as url params.


    Ok, creating some cache in window2 is not difficult.
    Just create an empty Object, and set some properties (names) that hold a
    value.

    Put something like this in window2 (not tested, syntax might suck, idea too)

    <script type="text/javascript">
    var myCache = new Object();

    // called from window1
    function doSet(name,value){
    myCache[name] = value;
    }

    // called from window1
    function doGet(name){
    return myCache[name];
    }
    </script>


    The hard part is telling the freshly reloaded window1 WHERE window2
    (your window with the cache) is.

    However: You CAN use window2 to find window1 using the opener keyword.

    Since it is not possible to get a direct reference to window2, you'll
    have to resort to some trick.
    eg: Let window2 make calls to window1 every second using the 'opener'
    keyword, trying to pass its own reference to window1, so window1 can use
    that.
    eg:
    opener.IAmHere(window);

    If window1 receives such a call, it stores the passed reference, so it
    knows how to find window2 for cache functionality.
    This would be a nice time too to tell window2 to stop telling what its
    reference is. ;-)

    Maybe add an onUnload eventhandler to window1, telling window2 to start
    polling again.
    You'll have to add erorhandling in case window2 makes a call to window1
    while it isn't fully loaded.

    Anyway, I think my above solution stinks and is a Bad Solution, but you
    might try it and see if it works.

    Things would be much easier if you could get a list of all
    windowreference opened by that window.
    eg: window.getAllOpenedWindows() or something, but as far as I know, no
    such functionality exists.

    Personally I would go to the server to store such information, but I
    don't know if that is a solution in your situation.

    Good luck.

    Maybe somebody knows another trick that my stupid polling-opener 'solution'.

    Regards,
    Erwin Moller

    >
    > any thoughts about this new data?
    >
    > thanks alot,
    > eyal.
    >
     
    Erwin Moller, Sep 10, 2007
    #4
  5. Eyal

    Eyal Guest

    On Sep 10, 3:03 pm, Erwin Moller
    <> wrote:
    > Eyal wrote:
    > > On Sep 10, 12:37 pm, Erwin Moller
    > > <> wrote:
    > >> Eyal wrote:
    > >>> hi all,
    > >>> i have researched the following problem and found no solution so i'm
    > >>> turning to u guys/girls for help.
    > >>> i am aware that my research could have been more complete but i have a
    > >>> dead-line to meet and i did put a few good hours into it to no avail.
    > >>> to the problem:
    > >>> frame1 creates an array called array1
    > >>> it then passes this array to frame2 by using: top.frame2["array1ref"]=
    > >>> array1;
    > >>> and then goes on to working with array1ref as needed.
    > >>> this is all good and dandy until i reload frame1.
    > >>> when reloading frame1 in firefox frame1 still has full access to
    > >>> array1ref.
    > >>> when doing the same in internet explorer 7 frame1 has only limited
    > >>> access to array1ref.
    > >>> specifically: it can read the array and use its elements but calling
    > >>> any member function of the array results in the following error
    > >>> message when debugging with visual studio 2003: "Unable to evaluate
    > >>> the expression. Catastrophic failure".
    > >>> any help will be greatly appreciated,
    > >>> eyal.
    > >> Hi,

    >
    > >> You created a somewhat strange situation.

    >
    > >> Consider by-value and by-reference:

    >
    > >> - Javascript passes around by-value and by-reference, depending on what
    > >> you are passing. Simple vars, like numbers and booleans, are passed
    > >> by-value.
    > >> Complex vars by reference: eg Arrays and Objects (Arrays ARE objects,
    > >> but leave that for now.)
    > >> And Strings are immutable, and for all practical purposes you don't have
    > >> to know HOW thay are passed in JavaScript (might even be implementation
    > >> dependant.)

    >
    > >> Here is a little simple JS that demonstrates some by-valu and some
    > >> by-ref behaviour.

    >
    > >> <html>
    > >> <head><title>test</title></head>
    > >> <body>

    >
    > >> <script type="text/javascript">

    >
    > >> function myFunc(whatAmI){
    > >> whatAmI = 10;

    >
    > >> }

    >
    > >> var simpleVar = 22;
    > >> myFunc(simpleVar);
    > >> alert ("example1: "+simpleVar);

    >
    > >> // This will alert 22, since myFunct received the value by-value, and
    > >> not by-reference.

    >
    > >> // Example 2: Passing by reference
    > >> function myFunc2(whatAmI){
    > >> whatAmI[0] = "test";

    >
    > >> }

    >
    > >> var complexVar = new Array(1,2,3,4);
    > >> myFunc2(complexVar);
    > >> alert ("example2: "+complexVar);

    >
    > >> // This will alert test,2,3,4

    >
    > >> // Example3:
    > >> // Actually things are a bit more complicated.
    > >> // If a function receives a variable by reference, it receives a copy of
    > >> the reference
    > >> // so overwriting the whole object fails outside the function. See in
    > >> this example
    > >> function myFunc3(whatAmI){
    > >> var useMe = new Array(100,200,300);
    > >> whatAmI = useMe;}

    >
    > >> var complexVar2 = new Array(-1,-2,-3);
    > >> myFunc3(complexVar2);
    > >> alert(complexVar2);

    >
    > >> </script>

    >
    > >> </body>
    > >> </html>

    >
    > >> To your question:
    > >> Window2 holds a reference to a datastructure (array) that is in window1.
    > >> You throw away window1.

    >
    > >> Now you created the situation that the browser must decide what to do
    > >> with the reference hold by a js-var in window2.
    > >> Whatever it holds (if any), it will NOT be the new one in window1.

    >
    > >> You also must consider the 'mark-and-sweep' garbage collection strategy
    > >> build in JS engines.
    > >> This garbage collection mechanism is responsibly for detecting that
    > >> nothing in an environment holds a reference to a certain structure, and
    > >> if so, it can free the memory allocated to this structure, since nothing
    > >> in the language can ever reach it anymore.
    > >> (This is done by simply counting the references to a certain
    > >> memorystructure).

    >
    > >> The best way out is avoiding this situation:
    > >> I would do it as follows:
    > >> - In window 2 NEVER store the reference, but simple address it again in
    > >> window1 when needed.
    > >> - Whenever window1 refreshes, make sure window2 is told to refresh
    > >> whatever it was it was doing, based on the new value of the reference.
    > >> (Probably use an onLoad event handler for this in window1)

    >
    > >> Hope this helps explaining why you see such weird behaviour.

    >
    > >> Regards,
    > >> Erwin Moller- Hide quoted text -

    >
    > >> - Show quoted text -

    >
    > > hi Erwin,

    >
    > > thanks alot for all the details.
    > > i understand the concepts of by-val and by-ref and was guessing along
    > > the lines of your explanation (though the part on the inner workings
    > > of the script engine is new to me and was a great lead).
    > > sadly the solution part that u offer in the end is not relevant in my
    > > case since window2's sole purpose is to cache vars so that they are
    > > present even if window1 is refreshed or even loads a totally new page.
    > > i was aiming at achieving a cookies' effect san the cookies.
    > > so actually i could have rephrased my problem to:
    > > "please help me find a replacement for cookies that gives me client
    > > side caching."

    >
    > Aha, that makes it a lot clearer. :)
    >
    > > the only other relatively simple way i can think of is passing the
    > > cached values to every page loaded as url params.

    >
    > Ok, creating some cache in window2 is not difficult.
    > Just create an empty Object, and set some properties (names) that hold a
    > value.
    >
    > Put something like this in window2 (not tested, syntax might suck, idea too)
    >
    > <script type="text/javascript">
    > var myCache = new Object();
    >
    > // called from window1
    > function doSet(name,value){
    > myCache[name] = value;
    > }
    >
    > // called from window1
    > function doGet(name){
    > return myCache[name];
    > }
    > </script>
    >
    > The hard part is telling the freshly reloaded window1 WHERE window2
    > (your window with the cache) is.
    >
    > However: You CAN use window2 to find window1 using the opener keyword.
    >
    > Since it is not possible to get a direct reference to window2, you'll
    > have to resort to some trick.
    > eg: Let window2 make calls to window1 every second using the 'opener'
    > keyword, trying to pass its own reference to window1, so window1 can use
    > that.
    > eg:
    > opener.IAmHere(window);
    >
    > If window1 receives such a call, it stores the passed reference, so it
    > knows how to find window2 for cache functionality.
    > This would be a nice time too to tell window2 to stop telling what its
    > reference is. ;-)
    >
    > Maybe add an onUnload eventhandler to window1, telling window2 to start
    > polling again.
    > You'll have to add erorhandling in case window2 makes a call to window1
    > while it isn't fully loaded.
    >
    > Anyway, I think my above solution stinks and is a Bad Solution, but you
    > might try it and see if it works.
    >
    > Things would be much easier if you could get a list of all
    > windowreference opened by that window.
    > eg: window.getAllOpenedWindows() or something, but as far as I know, no
    > such functionality exists.
    >
    > Personally I would go to the server to store such information, but I
    > don't know if that is a solution in your situation.
    >
    > Good luck.
    >
    > Maybe somebody knows another trick that my stupid polling-opener 'solution'.
    >
    > Regards,
    > Erwin Moller
    >
    >
    >
    >
    >
    > > any thoughts about this new data?

    >
    > > thanks alot,
    > > eyal.- Hide quoted text -

    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -


    thanks for the prompt response mate.
    i am about to try your solution but before that let me just ask a
    small Q:
    isn't your solution still dependent on an instance of a value (as in
    doSet(name,value)) that is in the window1 environment as was the case
    in my solution?
    it seems to me u just moved the problem to have an object wrapper
    (which might change something, i will check momentarilly) but the
    basic problem of window2 referencing an object created in window1
    remains, doesn't it?

    eyal.
     
    Eyal, Sep 10, 2007
    #5
  6. Eyal

    Eyal Guest

    On Sep 10, 3:03 pm, Erwin Moller
    <> wrote:
    > Eyal wrote:
    > > On Sep 10, 12:37 pm, Erwin Moller
    > > <> wrote:
    > >> Eyal wrote:
    > >>> hi all,
    > >>> i have researched the following problem and found no solution so i'm
    > >>> turning to u guys/girls for help.
    > >>> i am aware that my research could have been more complete but i have a
    > >>> dead-line to meet and i did put a few good hours into it to no avail.
    > >>> to the problem:
    > >>> frame1 creates an array called array1
    > >>> it then passes this array to frame2 by using: top.frame2["array1ref"]=
    > >>> array1;
    > >>> and then goes on to working with array1ref as needed.
    > >>> this is all good and dandy until i reload frame1.
    > >>> when reloading frame1 in firefox frame1 still has full access to
    > >>> array1ref.
    > >>> when doing the same in internet explorer 7 frame1 has only limited
    > >>> access to array1ref.
    > >>> specifically: it can read the array and use its elements but calling
    > >>> any member function of the array results in the following error
    > >>> message when debugging with visual studio 2003: "Unable to evaluate
    > >>> the expression. Catastrophic failure".
    > >>> any help will be greatly appreciated,
    > >>> eyal.
    > >> Hi,

    >
    > >> You created a somewhat strange situation.

    >
    > >> Consider by-value and by-reference:

    >
    > >> - Javascript passes around by-value and by-reference, depending on what
    > >> you are passing. Simple vars, like numbers and booleans, are passed
    > >> by-value.
    > >> Complex vars by reference: eg Arrays and Objects (Arrays ARE objects,
    > >> but leave that for now.)
    > >> And Strings are immutable, and for all practical purposes you don't have
    > >> to know HOW thay are passed in JavaScript (might even be implementation
    > >> dependant.)

    >
    > >> Here is a little simple JS that demonstrates some by-valu and some
    > >> by-ref behaviour.

    >
    > >> <html>
    > >> <head><title>test</title></head>
    > >> <body>

    >
    > >> <script type="text/javascript">

    >
    > >> function myFunc(whatAmI){
    > >> whatAmI = 10;

    >
    > >> }

    >
    > >> var simpleVar = 22;
    > >> myFunc(simpleVar);
    > >> alert ("example1: "+simpleVar);

    >
    > >> // This will alert 22, since myFunct received the value by-value, and
    > >> not by-reference.

    >
    > >> // Example 2: Passing by reference
    > >> function myFunc2(whatAmI){
    > >> whatAmI[0] = "test";

    >
    > >> }

    >
    > >> var complexVar = new Array(1,2,3,4);
    > >> myFunc2(complexVar);
    > >> alert ("example2: "+complexVar);

    >
    > >> // This will alert test,2,3,4

    >
    > >> // Example3:
    > >> // Actually things are a bit more complicated.
    > >> // If a function receives a variable by reference, it receives a copy of
    > >> the reference
    > >> // so overwriting the whole object fails outside the function. See in
    > >> this example
    > >> function myFunc3(whatAmI){
    > >> var useMe = new Array(100,200,300);
    > >> whatAmI = useMe;}

    >
    > >> var complexVar2 = new Array(-1,-2,-3);
    > >> myFunc3(complexVar2);
    > >> alert(complexVar2);

    >
    > >> </script>

    >
    > >> </body>
    > >> </html>

    >
    > >> To your question:
    > >> Window2 holds a reference to a datastructure (array) that is in window1.
    > >> You throw away window1.

    >
    > >> Now you created the situation that the browser must decide what to do
    > >> with the reference hold by a js-var in window2.
    > >> Whatever it holds (if any), it will NOT be the new one in window1.

    >
    > >> You also must consider the 'mark-and-sweep' garbage collection strategy
    > >> build in JS engines.
    > >> This garbage collection mechanism is responsibly for detecting that
    > >> nothing in an environment holds a reference to a certain structure, and
    > >> if so, it can free the memory allocated to this structure, since nothing
    > >> in the language can ever reach it anymore.
    > >> (This is done by simply counting the references to a certain
    > >> memorystructure).

    >
    > >> The best way out is avoiding this situation:
    > >> I would do it as follows:
    > >> - In window 2 NEVER store the reference, but simple address it again in
    > >> window1 when needed.
    > >> - Whenever window1 refreshes, make sure window2 is told to refresh
    > >> whatever it was it was doing, based on the new value of the reference.
    > >> (Probably use an onLoad event handler for this in window1)

    >
    > >> Hope this helps explaining why you see such weird behaviour.

    >
    > >> Regards,
    > >> Erwin Moller- Hide quoted text -

    >
    > >> - Show quoted text -

    >
    > > hi Erwin,

    >
    > > thanks alot for all the details.
    > > i understand the concepts of by-val and by-ref and was guessing along
    > > the lines of your explanation (though the part on the inner workings
    > > of the script engine is new to me and was a great lead).
    > > sadly the solution part that u offer in the end is not relevant in my
    > > case since window2's sole purpose is to cache vars so that they are
    > > present even if window1 is refreshed or even loads a totally new page.
    > > i was aiming at achieving a cookies' effect san the cookies.
    > > so actually i could have rephrased my problem to:
    > > "please help me find a replacement for cookies that gives me client
    > > side caching."

    >
    > Aha, that makes it a lot clearer. :)
    >
    > > the only other relatively simple way i can think of is passing the
    > > cached values to every page loaded as url params.

    >
    > Ok, creating some cache in window2 is not difficult.
    > Just create an empty Object, and set some properties (names) that hold a
    > value.
    >
    > Put something like this in window2 (not tested, syntax might suck, idea too)
    >
    > <script type="text/javascript">
    > var myCache = new Object();
    >
    > // called from window1
    > function doSet(name,value){
    > myCache[name] = value;
    > }
    >
    > // called from window1
    > function doGet(name){
    > return myCache[name];
    > }
    > </script>
    >
    > The hard part is telling the freshly reloaded window1 WHERE window2
    > (your window with the cache) is.
    >
    > However: You CAN use window2 to find window1 using the opener keyword.
    >
    > Since it is not possible to get a direct reference to window2, you'll
    > have to resort to some trick.
    > eg: Let window2 make calls to window1 every second using the 'opener'
    > keyword, trying to pass its own reference to window1, so window1 can use
    > that.
    > eg:
    > opener.IAmHere(window);
    >
    > If window1 receives such a call, it stores the passed reference, so it
    > knows how to find window2 for cache functionality.
    > This would be a nice time too to tell window2 to stop telling what its
    > reference is. ;-)
    >
    > Maybe add an onUnload eventhandler to window1, telling window2 to start
    > polling again.
    > You'll have to add erorhandling in case window2 makes a call to window1
    > while it isn't fully loaded.
    >
    > Anyway, I think my above solution stinks and is a Bad Solution, but you
    > might try it and see if it works.
    >
    > Things would be much easier if you could get a list of all
    > windowreference opened by that window.
    > eg: window.getAllOpenedWindows() or something, but as far as I know, no
    > such functionality exists.
    >
    > Personally I would go to the server to store such information, but I
    > don't know if that is a solution in your situation.
    >
    > Good luck.
    >
    > Maybe somebody knows another trick that my stupid polling-opener 'solution'.
    >
    > Regards,
    > Erwin Moller
    >
    >
    >
    >
    >
    > > any thoughts about this new data?

    >
    > > thanks alot,
    > > eyal.- Hide quoted text -

    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -


    as to referencing window2 from window1 when window1 just reloaded:
    that is not a problem since they are both a part of a constant
    frameset where each frame/window has its own unique name so u can
    always reference window2 by doing top.window2
     
    Eyal, Sep 10, 2007
    #6
  7. Eyal

    Eyal Guest

    On Sep 10, 3:03 pm, Erwin Moller
    <> wrote:
    > Eyal wrote:
    > > On Sep 10, 12:37 pm, Erwin Moller
    > > <> wrote:
    > >> Eyal wrote:
    > >>> hi all,
    > >>> i have researched the following problem and found no solution so i'm
    > >>> turning to u guys/girls for help.
    > >>> i am aware that my research could have been more complete but i have a
    > >>> dead-line to meet and i did put a few good hours into it to no avail.
    > >>> to the problem:
    > >>> frame1 creates an array called array1
    > >>> it then passes this array to frame2 by using: top.frame2["array1ref"]=
    > >>> array1;
    > >>> and then goes on to working with array1ref as needed.
    > >>> this is all good and dandy until i reload frame1.
    > >>> when reloading frame1 in firefox frame1 still has full access to
    > >>> array1ref.
    > >>> when doing the same in internet explorer 7 frame1 has only limited
    > >>> access to array1ref.
    > >>> specifically: it can read the array and use its elements but calling
    > >>> any member function of the array results in the following error
    > >>> message when debugging with visual studio 2003: "Unable to evaluate
    > >>> the expression. Catastrophic failure".
    > >>> any help will be greatly appreciated,
    > >>> eyal.
    > >> Hi,

    >
    > >> You created a somewhat strange situation.

    >
    > >> Consider by-value and by-reference:

    >
    > >> - Javascript passes around by-value and by-reference, depending on what
    > >> you are passing. Simple vars, like numbers and booleans, are passed
    > >> by-value.
    > >> Complex vars by reference: eg Arrays and Objects (Arrays ARE objects,
    > >> but leave that for now.)
    > >> And Strings are immutable, and for all practical purposes you don't have
    > >> to know HOW thay are passed in JavaScript (might even be implementation
    > >> dependant.)

    >
    > >> Here is a little simple JS that demonstrates some by-valu and some
    > >> by-ref behaviour.

    >
    > >> <html>
    > >> <head><title>test</title></head>
    > >> <body>

    >
    > >> <script type="text/javascript">

    >
    > >> function myFunc(whatAmI){
    > >> whatAmI = 10;

    >
    > >> }

    >
    > >> var simpleVar = 22;
    > >> myFunc(simpleVar);
    > >> alert ("example1: "+simpleVar);

    >
    > >> // This will alert 22, since myFunct received the value by-value, and
    > >> not by-reference.

    >
    > >> // Example 2: Passing by reference
    > >> function myFunc2(whatAmI){
    > >> whatAmI[0] = "test";

    >
    > >> }

    >
    > >> var complexVar = new Array(1,2,3,4);
    > >> myFunc2(complexVar);
    > >> alert ("example2: "+complexVar);

    >
    > >> // This will alert test,2,3,4

    >
    > >> // Example3:
    > >> // Actually things are a bit more complicated.
    > >> // If a function receives a variable by reference, it receives a copy of
    > >> the reference
    > >> // so overwriting the whole object fails outside the function. See in
    > >> this example
    > >> function myFunc3(whatAmI){
    > >> var useMe = new Array(100,200,300);
    > >> whatAmI = useMe;}

    >
    > >> var complexVar2 = new Array(-1,-2,-3);
    > >> myFunc3(complexVar2);
    > >> alert(complexVar2);

    >
    > >> </script>

    >
    > >> </body>
    > >> </html>

    >
    > >> To your question:
    > >> Window2 holds a reference to a datastructure (array) that is in window1.
    > >> You throw away window1.

    >
    > >> Now you created the situation that the browser must decide what to do
    > >> with the reference hold by a js-var in window2.
    > >> Whatever it holds (if any), it will NOT be the new one in window1.

    >
    > >> You also must consider the 'mark-and-sweep' garbage collection strategy
    > >> build in JS engines.
    > >> This garbage collection mechanism is responsibly for detecting that
    > >> nothing in an environment holds a reference to a certain structure, and
    > >> if so, it can free the memory allocated to this structure, since nothing
    > >> in the language can ever reach it anymore.
    > >> (This is done by simply counting the references to a certain
    > >> memorystructure).

    >
    > >> The best way out is avoiding this situation:
    > >> I would do it as follows:
    > >> - In window 2 NEVER store the reference, but simple address it again in
    > >> window1 when needed.
    > >> - Whenever window1 refreshes, make sure window2 is told to refresh
    > >> whatever it was it was doing, based on the new value of the reference.
    > >> (Probably use an onLoad event handler for this in window1)

    >
    > >> Hope this helps explaining why you see such weird behaviour.

    >
    > >> Regards,
    > >> Erwin Moller- Hide quoted text -

    >
    > >> - Show quoted text -

    >
    > > hi Erwin,

    >
    > > thanks alot for all the details.
    > > i understand the concepts of by-val and by-ref and was guessing along
    > > the lines of your explanation (though the part on the inner workings
    > > of the script engine is new to me and was a great lead).
    > > sadly the solution part that u offer in the end is not relevant in my
    > > case since window2's sole purpose is to cache vars so that they are
    > > present even if window1 is refreshed or even loads a totally new page.
    > > i was aiming at achieving a cookies' effect san the cookies.
    > > so actually i could have rephrased my problem to:
    > > "please help me find a replacement for cookies that gives me client
    > > side caching."

    >
    > Aha, that makes it a lot clearer. :)
    >
    > > the only other relatively simple way i can think of is passing the
    > > cached values to every page loaded as url params.

    >
    > Ok, creating some cache in window2 is not difficult.
    > Just create an empty Object, and set some properties (names) that hold a
    > value.
    >
    > Put something like this in window2 (not tested, syntax might suck, idea too)
    >
    > <script type="text/javascript">
    > var myCache = new Object();
    >
    > // called from window1
    > function doSet(name,value){
    > myCache[name] = value;
    > }
    >
    > // called from window1
    > function doGet(name){
    > return myCache[name];
    > }
    > </script>
    >
    > The hard part is telling the freshly reloaded window1 WHERE window2
    > (your window with the cache) is.
    >
    > However: You CAN use window2 to find window1 using the opener keyword.
    >
    > Since it is not possible to get a direct reference to window2, you'll
    > have to resort to some trick.
    > eg: Let window2 make calls to window1 every second using the 'opener'
    > keyword, trying to pass its own reference to window1, so window1 can use
    > that.
    > eg:
    > opener.IAmHere(window);
    >
    > If window1 receives such a call, it stores the passed reference, so it
    > knows how to find window2 for cache functionality.
    > This would be a nice time too to tell window2 to stop telling what its
    > reference is. ;-)
    >
    > Maybe add an onUnload eventhandler to window1, telling window2 to start
    > polling again.
    > You'll have to add erorhandling in case window2 makes a call to window1
    > while it isn't fully loaded.
    >
    > Anyway, I think my above solution stinks and is a Bad Solution, but you
    > might try it and see if it works.
    >
    > Things would be much easier if you could get a list of all
    > windowreference opened by that window.
    > eg: window.getAllOpenedWindows() or something, but as far as I know, no
    > such functionality exists.
    >
    > Personally I would go to the server to store such information, but I
    > don't know if that is a solution in your situation.
    >
    > Good luck.
    >
    > Maybe somebody knows another trick that my stupid polling-opener 'solution'.
    >
    > Regards,
    > Erwin Moller
    >
    >
    >
    >
    >
    > > any thoughts about this new data?

    >
    > > thanks alot,
    > > eyal.- Hide quoted text -

    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -


    hi Erwin,
    sadly your idea of wraping the cached variables in a cache object
    didn't solve the problem of arrays getting corrupted.

    :(

    i am about to try a private solution for arrays where i will copy the
    array element by element (assuming it only holds primitive types) in
    the window2 environment thus creating a copy that should not be
    corrupted when window1 refreshes.

    laters...
     
    Eyal, Sep 10, 2007
    #7
  8. Eyal

    Erwin Moller Guest

    Re: array passed from one frame to another gets corrupted when thefirst frame is reloaded.

    Eyal wrote:

    <snip>

    > thanks for the prompt response mate.
    > i am about to try your solution but before that let me just ask a
    > small Q:
    > isn't your solution still dependent on an instance of a value (as in
    > doSet(name,value)) that is in the window1 environment as was the case
    > in my solution?
    > it seems to me u just moved the problem to have an object wrapper
    > (which might change something, i will check momentarilly) but the
    > basic problem of window2 referencing an object created in window1
    > remains, doesn't it?


    Correct, but I addressed that too didn't I?
    The 'storing' part is simple on window2. Getting the reference is harder.

    Regards,
    Erwin Moller

    >
    > eyal.
    >
     
    Erwin Moller, Sep 10, 2007
    #8
  9. Eyal

    Erwin Moller Guest

    Re: array passed from one frame to another gets corrupted when thefirst frame is reloaded.

    Eyal wrote:

    <snip>

    >
    > as to referencing window2 from window1 when window1 just reloaded:
    > that is not a problem since they are both a part of a constant
    > frameset where each frame/window has its own unique name so u can
    > always reference window2 by doing top.window2
    >


    Nice for you!
    Problem solved.

    If you have a frameset, you don't need my stinking technique with
    polling. Just use parent.frames... or what you described.

    That makes it a lot easier. ;-)

    Regards,
    Erwin Moller
     
    Erwin Moller, Sep 10, 2007
    #9
  10. Eyal

    Erwin Moller Guest

    Re: array passed from one frame to another gets corrupted when thefirst frame is reloaded.

    Eyal wrote:

    <snip>

    >
    > hi Erwin,
    > sadly your idea of wraping the cached variables in a cache object
    > didn't solve the problem of arrays getting corrupted.


    The arrays get corrupted because the original window is reloaded.
    Thus the reference to the array is destroyed.
    window2 SHOULD copy it, or window2 should make sure it has a fresh
    reference, simply by asking window1 for the reference, not storing the
    reference.


    >
    > :(
    >
    > i am about to try a private solution for arrays where i will copy the
    > array element by element (assuming it only holds primitive types) in
    > the window2 environment thus creating a copy that should not be
    > corrupted when window1 refreshes.


    Good. If you make sure your original array contains only simple
    variables, you should be able to copy them without creating invalid
    references.

    If your original array contains references, redesign around it. These
    references might very well get corrupted too.

    An example:

    // bad
    myCache.mainDiv = document.getElementById("myMainDiv");

    // good:
    myCache.mainDivName = "myMainDiv";

    and then make sure you do a fresh document.getElementById on the name it
    holds ("myMainDiv").

    Bottomline: Make sure you do not store rererences in your custum build
    cache, only simple vars.

    Good luck.

    Regards,
    Erwin Moller



    >
    > laters...
    >
     
    Erwin Moller, Sep 10, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dee
    Replies:
    9
    Views:
    528
    Joseph Byrns
    Apr 15, 2005
  2. Jerry
    Replies:
    1
    Views:
    459
    Winista
    Jun 1, 2006
  3. Paul
    Replies:
    2
    Views:
    302
  4. Jerry
    Replies:
    1
    Views:
    149
    Winista
    Jun 1, 2006
  5. Harald Henkel

    frame reload() -> reloaded frame gets the focus

    Harald Henkel, Nov 10, 2003, in forum: Javascript
    Replies:
    0
    Views:
    278
    Harald Henkel
    Nov 10, 2003
Loading...

Share This Page