开发者

How to read an HTML cookie in Flash?

I'm hoping to get pointed in the right direction here. The problem I'm having is trying to figure out how to read an HTML created cookie in Flash. I have a video player that should autoplay once in a 24hour period, the next day it should autoplay again for the end-user.

This is what the script on the HTML page looks like that displays the Flash player and the cookie:

<script type="text/javascript">
var so = new SWFObject("flvplayer.swf", "mymovie", "640", "394", "开发者_Go百科8", "#90ab69");
var x = readCookie('homepageIntro') // <- The Cookie (How do I read this in Flash?)
so.addParam("quality", "high");
so.addParam("wmode", "transparent");
so.useExpressInstall('expressinstall.swf');
so.addVariable("autostart", "false");
so.addVariable("file", "video.flv");
so.addVariable("key", "");
so.addVariable("showfsbutton", "false");
so.addVariable("noControls", "false");
so.addVariable("home", "true");
so.write("flashcontent");
</script>

Not knowing how to read that var x inside of Flash, I tried to get around having to use it by using a Flash cookie, however now the video player will only ever autoplay once and never ever autoplay again(unable to clear the Flash cookie).

public function sharedObjectCheck():void
{
    if (mySharedObject.data.flashCookie == "true"){
        //Code to NOT autoplay video
    } else if (mySharedObject.data.flashCookie == null){
        mySharedObject.data.flashCookie = "true"; //if first time, set the cookie value
        mySharedObject.flush(); //add the cookie
    }
}

I did some searching and found this Have a HTML page play Flash movie only once (not when revisited…) but again this is just a Flash function which never allows for a restart in a certain time period.

So my question to my fellow Flash stackers is how do I read that var x(cookie) in Flash?


A direct answer to your question is to use ExternalInterface

import flash.external.*

try {
  var cookie : String = ExternalInterface.call("readCookie", "homepageIntro") as String;
} catch (error : SecurityError) {
  trace("SecurityError:", e.message);
} catch (error : Error) {
  trace("Error:", e.message);
}

You likely also need to set allowScriptAccess to let the call run.

Using a LSO is probably your best option, or passing in the value of x as a FlashVar.


Have you tried assigning the x value to flashvars?

so.addVariable("cookie", x);

In which case , you should be able to retrieve it in Flash , by doing so in the Document Class:

var params:Object = this.loaderInfo.parameters;
var cookie:Object = params.cookie;


Do it with a flash cookie, but actually store the last time autoplayed, rather than a simple flag.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜