Create a Flash SharedObject (Flash Cookie)


/ Published in: ActionScript 3
Save to your folder(s)

This snippet will help create a flashSharedObject a.k.a Flash Cookie, which much better than conventional cookies since they remain through all browsers and cannot be erased by the user using traditional cookie erasing methods


Copy this code and paste it in your HTML
  1. //create SO
  2. var mySharedObject:SharedObject = SharedObject.getLocal("republicofcode");
  3. mySharedObject.data.firstName = "John";
  4. mySharedObject.data.lastName = "Doe";
  5. mySharedObject.flush();
  6.  
  7. //Read SO
  8. var mySharedObject:SharedObject = SharedObject.getLocal("republicofcode");
  9. trace(mySharedObject.data.firstName);
  10. trace(mySharedObject.data.lastName);
  11.  
  12.  
  13. //Delete SO
  14. var mySharedObject:SharedObject = SharedObject.getLocal("republicofcode");
  15. mySharedObject.clear();
  16.  
  17.  
  18. //example for SO where it stored a X & Y coordinates of a movie
  19. ////////////////////////////////////////////////////////////////
  20.  
  21.  
  22. var mySO:SharedObject = SharedObject.getLocal("republicofcode");
  23.  
  24. movie_mc.x = mySO.data.my_x;
  25. movie_mc.y = mySO.data.my_y;
  26.  
  27. if (!mySO.data.my_y) {
  28. movie_mc.x = 150;
  29. movie_mc.y = 100;
  30. }
  31.  
  32. movie_mc.addEventListener (MouseEvent.MOUSE_DOWN, onDown);
  33. function onDown (e:MouseEvent):void {
  34. var my_mc = e.target;
  35. my_mc.startDrag ();
  36. }
  37.  
  38. movie.addEventListener (MouseEvent.MOUSE_UP, onUP);
  39. function onUP (e:MouseEvent):void {
  40. logo_mc.stopDrag ();
  41. mySO.data.my_x = movie_mc.x;
  42. mySO.data.my_y = movie_mc.y;
  43. mySO.flush ();
  44. }
  45.  
  46. movie_mc.buttonMode=true;

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.