我如何在 cookie 中设置值并从 cookie 获取值而不与 Flex 中的共享对象一起使用?

发布于 2024-09-01 15:16:27 字数 5159 浏览 2 评论 0原文

您好,我需要通过 cookie 将电子邮件 ID 保存在我的登录表单中。如果我使用共享对象,我可以保存,但我的要求是需要保存在cookie中。我该如何保存?我从网上得到了示例代码。附加该代码 `package com {

import flash.external.ExternalInterface; 

/** 
 * The Cookie class provides a simple way to create or access 
 * cookies in the embedding HTML document of the application. 
 *  
 */ 
public class Cookie { 

    /** 
     * Flag if the class was properly initialized. 
     */ 
    private static var _initialized:Boolean = false; 

    /** 
     * Name of the cookie. 
     */ 
    private var _name:String; 

    /** 
     * Contents of the cookie. 
     */ 
    private var _value:String; 

    /** 
     * Flag indicating if a cookie was just created. It is <code>true</code> 
     * when the cookie did not exist before and <code>false</code> otherwise. 
     */ 
    private var _isNew:Boolean; 

    /** 
     * Name of the external javascript function used for getting 
     * cookie information. 
     */ 
    private static const GET_COOKIE:String = "cookieGetCookie"; 

    /** 
     * Name of the external javascript function used for setting 
     * cookie information. 
     */ 
    private static const SET_COOKIE:String = "cookieSetCookie"; 

    /** 
     * Javascript code to define the GET_COOKIE function. 
     */ 
    private static var FUNCTION_GET_COOKIE:String = 
            "function () { " + 
                    "if (document." + GET_COOKIE + " == null) {" + 
                            GET_COOKIE + " = function (name) { " +  
                                    "if (document.cookie) {" +  
                                            "cookies = document.cookie.split('; ');" +  
                                            "for (i = 0; i < cookies.length; i++) {" +  
                                                    "param = cookies[i].split('=', 2);" +  
                                                    "if (decodeURIComponent(param[0]) == name) {" +  
                                                            "value = decodeURIComponent(param[1]);" +  
                                                            "return value;" +  
                                                    "}" +  
                                            "}" +  
                                    "}" +  
                                    "return null;" +  
                            "};" + 
                    "}" + 
            "}"; 

    /** 
     * Javascript code to define the SET_COOKIE function. 
     */ 
    private static var FUNCTION_SET_COOKIE:String = 
            "function () { " + 
                    "if (document." + SET_COOKIE + " == null) {" + 
                            SET_COOKIE + " = function (name, value) { " +  
                                    "document.cookie = name + '=' + value;" +  
                            "};" + 
                    "}" + 
            "}"; 

    /** 
     * Initializes the class by injecting javascript code into 
     * the embedding document. If the class was already initialized 
     * before, this method does nothing. 
     */ 
    private static function initialize():void { 
            if (Cookie._initialized) { 
                    return; 
            } 

            if (!ExternalInterface.available) { 
                    throw new Error("ExternalInterface is not available in this container. Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater, or other browsers that support NPRuntime are required."); 
            } 

            // Add functions to DOM if they aren't already there 
            ExternalInterface.call(FUNCTION_GET_COOKIE); 
            ExternalInterface.call(FUNCTION_SET_COOKIE); 

            Cookie._initialized = true; 
    } 

    /** 
     * Creates a new Cookie object. If a cookie with the specified 
     * name already exists, the existing value is used. Otherwise 
     * a new cookie is created as soon as a value is assigned to it. 
     *  
     * @param name The name of the cookie 
     */ 
    public function Cookie(name:String) { 
            Cookie.initialize(); 

            this._name = name; 
            this._value = ExternalInterface.call(GET_COOKIE, name) as String; 

            this._isNew = this._value == null; 
    } 

    /** 
     * The name of the cookie. 
     */ 
    public function get name():String { 
            return this._name; 
    } 

    /** 
     * The value of the cookie. If it is a new cookie, it is not 
     * made persistent until a value is assigned to it. 
     */ 
    public function get value():String { 
            return this._value; 
    } 

    /** 
     * @private 
     */ 
    public function set value(value:String):void { 
            this._value = value; 

            ExternalInterface.call(SET_COOKIE, this._name, this._value); 
    } 

    /** 
     * The <code>isNew</code> property indicates if the cookie 
     * already exists or not. 
     */ 
    public function get isNew():Boolean { 
            return this._isNew; 
    } 
} 

} 「我该如何使用这个?我需要将其与jsp集成,默认swf文件嵌入在jsp中。 如何只保存用户名(电子邮件 ID)?如果用户再次重新输入,它应该显示在下拉列表中。如何将文本输入文本传递到 cookie 中?请提前帮助我。

Hi I need to save email-id in my login form through the cookies. if I use shared object I am able to save but my requirement is need to save in cookies. How can I save? I got sample code from net. Attaching that code `package com {

import flash.external.ExternalInterface; 

/** 
 * The Cookie class provides a simple way to create or access 
 * cookies in the embedding HTML document of the application. 
 *  
 */ 
public class Cookie { 

    /** 
     * Flag if the class was properly initialized. 
     */ 
    private static var _initialized:Boolean = false; 

    /** 
     * Name of the cookie. 
     */ 
    private var _name:String; 

    /** 
     * Contents of the cookie. 
     */ 
    private var _value:String; 

    /** 
     * Flag indicating if a cookie was just created. It is <code>true</code> 
     * when the cookie did not exist before and <code>false</code> otherwise. 
     */ 
    private var _isNew:Boolean; 

    /** 
     * Name of the external javascript function used for getting 
     * cookie information. 
     */ 
    private static const GET_COOKIE:String = "cookieGetCookie"; 

    /** 
     * Name of the external javascript function used for setting 
     * cookie information. 
     */ 
    private static const SET_COOKIE:String = "cookieSetCookie"; 

    /** 
     * Javascript code to define the GET_COOKIE function. 
     */ 
    private static var FUNCTION_GET_COOKIE:String = 
            "function () { " + 
                    "if (document." + GET_COOKIE + " == null) {" + 
                            GET_COOKIE + " = function (name) { " +  
                                    "if (document.cookie) {" +  
                                            "cookies = document.cookie.split('; ');" +  
                                            "for (i = 0; i < cookies.length; i++) {" +  
                                                    "param = cookies[i].split('=', 2);" +  
                                                    "if (decodeURIComponent(param[0]) == name) {" +  
                                                            "value = decodeURIComponent(param[1]);" +  
                                                            "return value;" +  
                                                    "}" +  
                                            "}" +  
                                    "}" +  
                                    "return null;" +  
                            "};" + 
                    "}" + 
            "}"; 

    /** 
     * Javascript code to define the SET_COOKIE function. 
     */ 
    private static var FUNCTION_SET_COOKIE:String = 
            "function () { " + 
                    "if (document." + SET_COOKIE + " == null) {" + 
                            SET_COOKIE + " = function (name, value) { " +  
                                    "document.cookie = name + '=' + value;" +  
                            "};" + 
                    "}" + 
            "}"; 

    /** 
     * Initializes the class by injecting javascript code into 
     * the embedding document. If the class was already initialized 
     * before, this method does nothing. 
     */ 
    private static function initialize():void { 
            if (Cookie._initialized) { 
                    return; 
            } 

            if (!ExternalInterface.available) { 
                    throw new Error("ExternalInterface is not available in this container. Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater, or other browsers that support NPRuntime are required."); 
            } 

            // Add functions to DOM if they aren't already there 
            ExternalInterface.call(FUNCTION_GET_COOKIE); 
            ExternalInterface.call(FUNCTION_SET_COOKIE); 

            Cookie._initialized = true; 
    } 

    /** 
     * Creates a new Cookie object. If a cookie with the specified 
     * name already exists, the existing value is used. Otherwise 
     * a new cookie is created as soon as a value is assigned to it. 
     *  
     * @param name The name of the cookie 
     */ 
    public function Cookie(name:String) { 
            Cookie.initialize(); 

            this._name = name; 
            this._value = ExternalInterface.call(GET_COOKIE, name) as String; 

            this._isNew = this._value == null; 
    } 

    /** 
     * The name of the cookie. 
     */ 
    public function get name():String { 
            return this._name; 
    } 

    /** 
     * The value of the cookie. If it is a new cookie, it is not 
     * made persistent until a value is assigned to it. 
     */ 
    public function get value():String { 
            return this._value; 
    } 

    /** 
     * @private 
     */ 
    public function set value(value:String):void { 
            this._value = value; 

            ExternalInterface.call(SET_COOKIE, this._name, this._value); 
    } 

    /** 
     * The <code>isNew</code> property indicates if the cookie 
     * already exists or not. 
     */ 
    public function get isNew():Boolean { 
            return this._isNew; 
    } 
} 

}
`How can I use this? I need to integrate this with jsp by default swf file is embedded in jsp.
How can I save only username (email-id)? If user reenters again it should show in dropdown. how can I pass text input text into the cookie? Please help me in these Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚雾 2024-09-08 15:16:27

发布的代码使用 ExternalInterface< /a> 类通过 JavaScript 访问浏览器 cookie。我想你的问题是关于如何使用该类。给你:

// Initialize the object: 
var cookie:Cookie = new Cookie("NAME OF THE COOKIE");

// Retrieve and trace the value of the cookie:
trace(cookie.value());

// Set new value for the cookie:
cookie.value = "NEW VALUE";

我无法回答有关下拉列表的其余问题。您需要更具体地说明您的实际问题是什么。

The posted code uses the ExternalInterface class to access the browsers cookies through JavaScript. I guess your question is about how to use that class. Here you go:

// Initialize the object: 
var cookie:Cookie = new Cookie("NAME OF THE COOKIE");

// Retrieve and trace the value of the cookie:
trace(cookie.value());

// Set new value for the cookie:
cookie.value = "NEW VALUE";

I cannot answer the remaining questions about the drop down. You'll need to be more specific what your actual question is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文