如何访问在Flash cookies吗? [英] How do I access cookies within Flash?

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

问题描述

我期待抓住cookie值的Flash影片中的同一个域。这可能吗?

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类。请参见 Adob​​e的共享对象参考获得的更多细节。

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 cookies,您将需要使用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);
    }
}

您需要确保您使用的allowScriptAccess参数在您的Flash对象启用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();
        }
    }
}

一旦你有HTTPCookies.as在您的Flash项目,HTTPCookies.js从你的网页加载,你应该能够调用的getCookie和setCookie方法从您的Flash影片中要获取或设置的HTTP cookies。

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.

这将仅适用于非常简单的数值 - 字符串或数字 - 但任何事情你真的应该使用共享对象更复杂

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

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

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