如何访问Flash中的Cookie? [英] How do I access cookies within Flash?

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

问题描述

我想要在Flash电影中抓取同一网域的Cookie值。这是可能吗?

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

让我们看看我让用户设置一个变量foo,并使用任何Web编程语言存储它。我可以通过该语言轻松访问它,但我想通过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类。有关详情,请参见 Adob​​e的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);
    }
}

您需要确保使用'

然后你需要创建一对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();
        }
    }
}

一旦你有HTTPCookies.as在您的flash项目中,以及从您的网页加载的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中的Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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