04 февруари 2009, сряда

Flash 8 and Cookies

The following lines present how to get and save cookies with ActionScript 2.0 for Flash ver. 8 and above:

import flash.external.ExternalInterface;


function SetCookie(cookieName, cookieValue, cookieExpires) {

    js = "function sc(){";

    if (cookieExpires) {

        js += "howlong=new Date(); howlong.setTime(howlong.getTime() + " + cookieExpires + "000);";

    }

    js += "var c = escape('" + cookieName + "') + '=' + escape('" + cookieValue + "') + '; ";

    if (cookieExpires) {

        js += "{expires=' + howlong.toGMTString() + '; ";

    }

    js += "path=/';";

    js += "document.cookie = c;";

    js += "}";

    ExternalInterface.call(js);

}


function GetCookie(cookieName) {

    var r = "";

    var search = cookieName + "=";

    var js = "function get_cookie(){return document.cookie;}";

    var cookieVariable = ExternalInterface.call(js).toString();

    if (cookieVariable.length > 0) {

        offset = cookieVariable.indexOf(search);

        if (offset != -1) {

            offset += search.length;

            end = cookieVariable.indexOf(";", offset);

            if (end == -1) {

                end = cookieVariable.length;

            }

            r = unescape(cookieVariable.substring(offset, end));

        }

    }

    return r;

}

1 коментара:

Анонимен каза...

I must digg your post therefore other folks can see it, really useful, I had a hard time finding the results searching on the web, thanks.

- Joe