Javascript to Flash communication doesn't work
I'm trying to control a flash player from javascript, i did as i saw on the internet and i get an "not supported" error
here what i've wrote:
on js:
function getFlashMovieObject(movieName)
{
if (wind开发者_如何转开发ow.document[movieName])
{
return window.document[movieName];
}
if (navigator.appName.indexOf("Microsoft Internet")==-1)
{
if (document.embeds && document.embeds[movieName])
return document.embeds[movieName];
}
else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
{
return document.getElementById(movieName);
}
}
function SetNum1()
{
var x=getFlashMovieObject("flashmovie");
x.Setvariable("z0", "Z0");
//document.getElementById("flashmovie").setVariable("z0", "Z0");
alert("hi");
}
on html:
<object id="flashmovie" width="40" height="300">
<param name="movie" value="complex Ex A2P.swf">
<embed src="complex Ex A2P.swf" width="400" height="300">
</embed>
</object>
note : I tryed "Setvariable", "setvariable" , "SetVariable" and "setVariable" (diffrence in capital letter)
You can call the JS function SetNum1
after the DOM has loaded - the easiest way to be sure is to put the script just before </body>
tag.
Also, you need to make sure that you have enabled Setvariable
function in the AS code:
ExternalInterface.addCalback("Setvariable", setVariable);
//the second parameter is the function name in the Actionscript code
Several things in addition to the previous recomendations:
You'll need to make sure your flash movie has
<param name="allowscriptaccess" value="always">
in the embed code. If that doesn't work, try logging out the function in the javascript to make sure it exists before calling it as in
var x=getFlashMovieObject("flashmovie");
console.log("function", x.Setvariable); // see what you get in your console log here
If you see undefined in the console, chances are you have a sequencing issue and will need to change your order of execution to make sure your swf exists and the callback has been added before it's called. A lot happens in that split second in initialization and you want to make sure things happen in the correct order.
lastly, if this still doesn't work, there may be a security problem which you can quick fix by adding
Security.allowDomain('*');
Just under your class definition (or wherever you are storing your code) in the actionscript. If this last item fixes the problem, you might want to look into Security.allowDomain , particularly if you are using ExternalInterface and you might be worried about cross site scripting attacks. In most cases, this is fine but it can end up being very bad if your swf has access to your database, will be loaded by other sites, or areas of your own site that should be secure, so use the global solution above only with caution.
精彩评论