开发者

JavaScript variable to Flash?

I have this code in JavaScript with which I want to send a variable to Flash:

function getMovie()
{
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window["Hra_Kraje.swf"] : document["Hra_Kr开发者_Python百科aje.swf"];
}


function getUser(user)
{  
    Name = user.name;
    getMovie().sendTextToFlash(Name); 
}

and the following AS2 code in Flash:

import flash.external.ExternalInterface;

function getTextFromJavaScript(str:String):Void
{
    name = str;
}

ExternalInterface.addCallback("sendTextToFlash", this, getTextFromJavaScript);

But the movie does not seem to recieve the variable. Where is the problem please?


Unless you're using a dynamic Flash object embedding method, your Flash object HTML should look something like this:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="" id="sample" width=400 height=80>
    <param name="movie" VALUE="sample.swf">
    <embed play="false" swliveconnect="true" name="sample" src="sample.swf" quality="high" bgcolor="#FFFFFF" width=400 height=80 type="application/x-shockwave-flash"></embed>
</object>

The id="sample in the object tag and name="sample" & swliveconnect="true" in the embed tag are important. The object tag's ID value and the embed tag's name value should be the same (but doesn't have to be "sample" of course). You'll use that value in your window["sample"] and document["sample"] calls, like so:

function changetext(str){   
    if(window.sample) window.document["sample"].SetVariable("myText", str);
    if(document.sample) document.sample.SetVariable("myText", str);
}

The simple example above uses a SetVariable callback method that is in the Flash movie. In your case, it should just be your sendTextToFlash callback, if your AS2 code is correct, which appears to be the case. Try fixing your HTML code first, and see if that works, then report back.

You might also check out these two resources for more details and working demo code: http://www.oddhammer.com/tutorials/firefox_setvariable/
http://www.permadi.com/tutorial/flashjscommand/


You are trying to get DOMElement for the movie using its filename

window["Hra_Kraje.swf"] : document["Hra_Kraje.swf"]

This wont work. Instead you should use the "id"

window[id_of_object_or_embed_tag] : document[id_of_object_or_embed_tag]

Even better, use something like swfobject for placing the movie on the page

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜