开发者

restoring scroll position in Flex Web Page

This is a fairly crucial but overlooked functionality I guess in a web page - on a standard (non flex) web page, navigate away from the page and return to it via the back or forward button and it restores the scroll position you wer开发者_Python百科e at on the page previously. This is a crucial function and it would get very annoying if it wasn't there - having a page automatically go back to the very top each time when you were previously scrolled way down on a long text site. I guess the browser itself must be managing this,as I was surprised to find that even deleting cookies, the cache and basically everything will not get rid of that saved scroll position.

So the problem is doing that on a flex web page - what is the correct way to do it. I have been using SHaredObject.getLocal, but was dumbfounded to discover that once a local shared object is created , it will NEVER be deleted, there is nothing a user can do in the browser to delete them (deleting cookies, etc has no effect.) So I had like 100 different sharedobjects in a Macromedia subdirectory storing nothing but scroll positions. Even with standard web pages you can delete browsing history, but with SharedObject they're there for good unless you actually just delve into that file subdirectory and delete them manually (which of course a user would never do.) So a subquestion would be, is it really true that there is no way to delete SharedObjects from the browser. FLex even calls SharedObjects "Flash cookies" in their docs, but with real cookies the user can delete them.


My solution is to have just one SharedObject for all URL's generated, and this SHaredObject holds an object operating as a circular buffer. This object has properties indexed by URL, with each property pointing to the session info for that URL (e.g. scroll position, etc.). Haven't tested the following yet, but this is what I've got. If there is already a built in way in Flex to do a circular buffer in a local SharedObject I haven't found it.

function createSessionInfo(url:String,info:Object):void {  

  var so_obj:SharedObject = null;
  try { 
    so_obj = SharedObject.getLocal("Session_Info","/");
  } catch (e:*) {}          
  if (!so_obj) return;

  if (so_obj.data.session == undefined)
    so_obj.data.session = new Object();

  var session:Object = so_obj.data.session;

  var date:Date = new Date();
  var max:int = 100;
  var first:String;
  var first_created:int=date.time;
  var cnt:int=0
  for (var p:String in session) {
    if (session[p].created < first_created) {
      first_created = session[p].created;  
      first = p;
    }
    if (++cnt == max) 
      delete session[first];
  }

  session[url] = new Object();
  session[url].created = date.time;
  session[url].info = info;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜