如何在 Flash 中访问 c​​ookie? [英] How do I access cookies within Flash?

查看:22
本文介绍了如何在 Flash 中访问 c​​ookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望获取 Flash 电影中同一域的 cookie 值.这可能吗?

I'm looking to grab cookie values for the same domain within a Flash movie. Is this possible?

让我们看看我让用户设置一个变量 foo 并使用任何网络编程语言存储它.我可以通过该语言轻松访问它,但我想通过 Flash 电影访问它,而不是通过在 HTML 页面中打印它来传递它.

Let's see I let a user set a variable foo and I store it using any web programming language. I can access it easily via that language, but I would like to access it via the Flash movie without passing it in via printing it within the HTML page.

推荐答案

如果您只想存储和检索数据,您可能想使用 SharedObject 类.有关更多详细信息,请参阅 Adobe 的 SharedObject 参考.

If you just want to store and retrieve data, you probably want to use the SharedObject class. See Adobe's SharedObject reference for more details of that.

如果您想访问 HTTP cookie,您需要使用 ExternalInterface 与 javascript 对话.我们在这里这样做的方法是有一个名为 HTTPCookies 的帮助器类.

If you want to access the HTTP cookies, you'll need to use ExternalInterface to talk to javascript. The way we do that here is to have a helper class called HTTPCookies.

HTTPCookies.as:

HTTPCookies.as:

import flash.external.ExternalInterface;

public class HTTPCookies
{
    public static function getCookie(key:String):*
    {
        return ExternalInterface.call("getCookie", key);
    }

    public static function setCookie(key:String, val:*):void
    {
        ExternalInterface.call("setCookie", key, val);
    }
}

您需要确保使用 Flash 对象中的allowScriptAccess"参数启用 javascript.

You need to make sure you enable javascript using the 'allowScriptAccess' parameter in your flash object.

然后你需要创建一对javascript函数,getCookie和setCookie,如下(感谢quirksmode.org)

Then you need to create a pair of javascript functions, getCookie and setCookie, as follows (with thanks to quirksmode.org)

HTTPCookies.js:

HTTPCookies.js:

function getCookie(key)
{
    var cookieValue = null;

    if (key)
    {
        var cookieSearch = key + "=";

        if (document.cookie)
        {
            var cookieArray = document.cookie.split(";");
            for (var i = 0; i < cookieArray.length; i++)
            {
                var cookieString = cookieArray[i];

                // skip past leading spaces
                while (cookieString.charAt(0) == ' ')
                {
                    cookieString = cookieString.substr(1);
                }

                // extract the actual value
                if (cookieString.indexOf(cookieSearch) == 0)
                {
                    cookieValue = cookieString.substr(cookieSearch.length);
                }
            }
        }
    }

    return cookieValue;
}

function setCookie(key, val)
{
    if (key)
    {
        var date = new Date();

        if (val != null)
        {
            // expires in one year
            date.setTime(date.getTime() + (365*24*60*60*1000));
            document.cookie = key + "=" + val + "; expires=" + date.toGMTString();
        }
        else
        {
            // expires yesterday
            date.setTime(date.getTime() - (24*60*60*1000));
            document.cookie = key + "=; expires=" + date.toGMTString();
        }
    }
}

一旦您的 Flash 项目中有 HTTPCookies.as,并且从您的网页加载了 HTTPCookies.js,您应该能够从您的 Flash 影片中调用 getCookie 和 setCookie 来获取或设置 HTTP cookie.

Once you have HTTPCookies.as in your flash project, and HTTPCookies.js loaded from your web page, you should be able to call getCookie and setCookie from within your flash movie to get or set HTTP cookies.

这仅适用于非常简单的值 - 字符串或数字 - 但对于更复杂的任何内容,您确实应该使用 SharedObject.

This will only work for very simple values - strings or numbers - but for anything more complicated you really should be using SharedObject.

这篇关于如何在 Flash 中访问 c​​ookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆