Laan's steps..

Feed Rss

flash cookie读取类

05.19.2009, flash, js, my works, by .

闲来无聊,索性放上一个flash读取cookie的类。原理就是在flash里面调用js方法从而来获取或写入cookie。

Cookie:

package laan.js
{
import flash.external.ExternalInterface;

/**
* 页面cookie控制管理
*
*
* @author laan
* @createTime 2009.4
*
*/
public class Cookie
{
embedJS();

/**
* 绑定js
*
*/
private static function embedJS():void {
var str:String = “”;

//设置cookie
str += ‘function setCookie_(name, value, expires, security) {‘ +
‘    var str = name + “=” + escape(value);’ +
‘    if (expires != null) str += “;expires=” + new Date(expires).toGMTString() + “”;’ +
‘    if (security == true) str += “;secure”;’ +
‘    document.cookie = str;’ +
‘}’;

//获取cookie
str += ‘function getCookie_(name) {‘ +
‘    var arr = document.cookie.match(new RegExp(“;?” +name + “=([^;]*)”));’ +
‘    if(arr != null) return unescape(arr[1]); ‘ +
‘    return null;’ +
‘}’;

str += ‘function deleteCookie_(name) {‘ +
‘    var d = new Date();’ +
‘    d.setTime(d.getTime() – 1);’ +
‘    var value = getCookie_(name);’ +
‘    if(value != null) {‘ +
‘        alert(document.cookie);document.cookie = name + “=” + escape(value) + “;expire=” + d.toGMTString();’ +
‘    }’ +
‘}’;
if (ExternalInterface.available) {
ExternalInterface.call(“eval”, str);
}
}

/**
* 设置cookie
*
* @param name            cookie名称
* @param value            cookie值
* @param expires        cookie过期时间
* @param security        是否加密
*
*/
public static function setCookie(name:String, value:String, expires:Date = null, security:Boolean = false):void {
if (ExternalInterface.available) {
ExternalInterface.call(“setCookie_”, name, value, expires ? expires.time : expires, security);
}
}

/**
* 获取cookie值
*
* @param name
* @return
*
*/
public static function getCookie(name:String):String {
if (ExternalInterface.available) {
return ExternalInterface.call(“getCookie_”, name);
}

return null;
}

/**
* 删除cookie
*
* @param name
*
*/
public static function deleteCookie(name:String):void {
if (ExternalInterface.available) {
ExternalInterface.call(“deleteCookie_”, name);
}
}
}
}

flash cookie读取类 有 2 条回应

  1. # public static function setCookie(name:String, value:String, expiresate = null, security:Boolean = false):void {
    # if (ExternalInterface.available) {
    # ExternalInterface.call(“setCookie_”, name, value, expires ? expires.time : expires, security);
    # }
    # }

    勘误一下,函数声明的时候写expiresate,而函数体内却是expires.

    回复
  2. 我正好需要这样一个功能加入到我的网站中, 不知能否帮忙开发一下,可联系QQ:
    43930558

    回复

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>