JavaScriptやFlashには、HTTP cookieに情報を保存してユーザーのローカルマシンに情報を保存できるという便利な機能があります。
この情報は他のサイトに移動しても維持され、ブックマークや進行状況の追跡、パスワードやハイスコアといったユーザー情報の保存に利用されています。また、ユーザーインターフェイスの状態を保存するといった使い方もできます。
実際Flashのプロジェクト中に、
・1回目に見ときはイントロムービーが流れるように
・2回目以降はイントロムービーは流れずにスキップ
・ブラウザを終了させてもう一度開いたら、またイントロムービーが見れるように
というような希望も多いと思いますね。
JavaScriptならたいていスニペットコードを使い、以下のソースコードcookieを読み/書きするスニペットコードを元に作ったコードです。
- <script type="text/javascript">
- function writeCookie(name, value, hours)
- {
- var expire = "";
- if( hours != null )
- {
- expire = new Date((new Date()).getTime() + hours * 3600000);
- expire = "; expires=" + expire.toGMTString();
- }
- document.cookie = name + "=" + escape(value) + expire;
- }
- function readCookie(name)
- {
- var cookieValue = "";
- var search = name + "=";
- if(document.cookie.length > 0)
- {
- offset = document.cookie.indexOf(search);
- if( offset != -1 )
- {
- offset += search.length;
- end = document.cookie.indexOf(";", offset);
- if (end == -1) end = document.cookie.length;
- cookieValue = unescape(document.cookie.substring(offset, end))
- }
- }
- return cookieValue;
- }
- </script>
cookieに「userScore」という名前で「80」というスコアの値を保存したい場合は、以下のJavaScriptコードを追加してください。
- writeCookie("userScore", 80, 40);
ActionScriptも同じ機能を実現したい場合はどうするの?
以下はAlexanderさんが書かれたソースコードです。ここにクリックするとダウンロードできます。
- package de.aggro.utils
- {
- import flash.external.ExternalInterface;
- public class CookieUtil
- {
- public function CookieUtil()
- {
- }
- private static const FUNCTION_SETCOOKIE:String =
- "document.insertScript = function ()" +
- "{ " +
- "if (document.snw_setCookie==null)" +
- "{" +
- "snw_setCookie = function (name, value, days)" +
- "{" +
- "if (days) {"+
- "var date = new Date();"+
- "date.setTime(date.getTime()+(days*24*60*60*1000));"+
- "var expires = ‘; expires=’+date.toGMTString();"+
- "}" +
- "else var expires = ”;"+
- "document.cookie = name+’='+value+expires+’; path=/’;" +
- "}" +
- "}" +
- "}";
- private static const FUNCTION_GETCOOKIE:String =
- "document.insertScript = function ()" +
- "{ " +
- "if (document.snw_getCookie==null)" +
- "{" +
- "snw_getCookie = function (name)" +
- "{" +
- "var nameEQ = name + ‘=’;"+
- "var ca = document.cookie.split(’;');"+
- "for(var i=0;i < ca.length;i++) {"+
- "var c = ca[i];"+
- "while (c.charAt(0)==’ ‘) c = c.substring(1,c.length);"+
- "if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);"+
- "}"+
- "return null;" +
- "}" +
- "}" +
- "}";
- private static var INITIALIZED:Boolean = false;
- private static function init():void{
- ExternalInterface.call(FUNCTION_GETCOOKIE);
- ExternalInterface.call(FUNCTION_SETCOOKIE);
- INITIALIZED = true;
- }
- public static function setCookie(name:String, value:Object, days:int):void{
- if(!INITIALIZED)
- init();
- ExternalInterface.call("snw_setCookie", name, value, days);
- }
- public static function getCookie(name:String):Object{
- if(!INITIALIZED)
- init();
- return ExternalInterface.call("snw_getCookie", name);
- }
- public static function deleteCookie(name:String):void{
- if(!INITIALIZED)
- init();
- ExternalInterface.call("snw_setCookie", name, "", -1);
- }
- }
- }
使用例です。
- import de.aggro.utils.CookieUtil;
- //Set a cookie named mycookie with a value of mycookie value with a time to live of 30 days
- CookieUtil.setCookie("mycookie", "mycookie value", 30);
- //Get that cookie and trace its value
- trace(CookieUtil.getCookie("mycookie"));
- //Delete the cookie from the users computer
- CookieUtil.deleteCookie("mycookie");
メインコンテンツEND ■
Posted on Thursday, 10th December 2009 by admin
Tags: ActionScript, Cookie, クッキー
Posted in ActionScript | Comments (0) | 7,200 views
