Accessing browser cookies from Flex

帅比萌擦擦* 提交于 2019-11-27 15:30:43

I have never tried this, but this library might just do the trick.

Debarghya

As per the flash or flex cookies are concern developer can use shared object which is one kind of cookie used for flex application.

The sample code snippet is as followes

import flash.net.SharedObject;

// get/create the shared object with a unique name.
// If the shared object exists this grab it, if not
// then it will create a new one
var so: SharedObject = SharedObject.getLocal("UniqueName");

// the shared object has a propery named data, it's
// an object on which you can create, read, or modify
// properties (you can't set the data property itself!)
// you can check to see if it already has something set
// using hasOwnProperty, so we'll check if it has a var
// use it if it does, or set it to a default if it doesn't
if (so.data.hasOwnProperty("theProp"))
{
    trace("already has data! It reads: " + so.data.theProp);
}
else
{
    so.data.theProp = "default value";
    so.flush(); // flush saves the data
    trace("It didn't have a value, so we set it.");
}
Carlos Gili

Accessing Flex SharedObject is NOT the same as accessing the Browser cookies, to access the browser cookies, you may use the ExternalInterface class, please check the following reference to see samples:

http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_4.html

A reference of how to use and control cookies using JavaScript can be found here:

http://www.quirksmode.org/js/cookies.html

I would use the following Flex code:

var myCookie:String = ExternalInterface.call("getCookie('cookieName')");

And in the HTML I would add the following Javascript:

function getCookie(c_name) {
  var i,x,y,ARRcookies=document.cookie.split(";");
  for (i=0;i<ARRcookies.length;i++) {
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
    x=x.replace(/^\s+|\s+$/g,"");
    if (x==c_name) return unescape(y);
  }
}

If you require more help you could also check the Flex documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!