Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2009
    Location
    Florida, USA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    availability of Javascript object in the next page

    Hi,

    Can anyone please help me how to prevent/access the Javascript object for next page? means I have one javascript array which consists of multiple java script objects in it. so how can I use this Javascript array in the next page/request.

    usually if I have any javascript variable I assign to form hidden variable or attach to the URL, but here I have javascript array which consists of multiple javascript objects. So I heard we can solve this by using JSON or DWR etc..
    Is it the way or any other solution? So could anyone please help/drive me how can I approach this scenerio? or any sample code that would be greatly appreciated.

    thanks
    -Sri

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,399
    Thanks
    11
    Thanked 595 Times in 575 Posts
    //xbrowser, requires JSON2 library
    Code:
    window.onunload=function(){
      window.name = JSON.stringify(myOb);
    }
    
    window.onload=function(){
     myOb= JSON.parse(window.name || "{}") ;
    }
    
    myOb={status:"loading"};

    simple firefox version (tested FF3):
    Code:
    window.onunload=function(){
      window.name = myOb.toSource();
    }
    
    window.onload=function(){
     myOb= eval(window.name || "({})" ) ;
    }
    this will preserve the object myOb between pages, even domains, but not reboots...

    remember to do your homework on eval, any site can touch window.name.

    you can use this in an iframe to communicate between domains, each page setting the .name, and the .location (or .src from the top ), but it's a bit finiky, and it's difficult to time cross browser.

    for a one-way transfer, opening a new page with a bundle of data, window.name is the simplest and cheapest transport around.
    the xbrowser issue are much tamer one way than they are on the barely-legal iframe usage of this technique, it's a nifty little trick you don't see often enough.


    you can also set window.location.search to the JSON string, and parse it on boot. it's a lot better than a regular flat querystring, because it's strong typed, tree-shaped, and has built-in parsing for javascript via eval/JSON.
    only problem is that your limited to about 1.5-3.5 kb depending on data and browser, and proxies...

    window.name is much bigger


    you could wuss-out and use a lame-o server to do it, but how fun is that?
    Last edited by rnd me; 06-03-2009 at 06:54 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •