Adobe ExternalInterface not working in IE9
I am calling an Actionscript function from JavaScript and it works in all browsers except for IE9. I narrowed it down to the js function that retrieves the movie object:
<script type="text/javascript">
var swf;
...
function flashReady() // This is called from ActionScript
{
swf = getSWF("MyMovie");
swf.MyExternalFunction();
}
function getSWF(movieName)
{
if (navigator.appName.indexOf("Microsoft") != -1)
{
return window[movieName];
}
else
{
return document[movieName];
}
}
...
</script>
Here is my HTML:
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="1" height="1" id="MyMovie">
<param name="allowScriptAccess" value="always" />
<param name="movie" value="/swf/movie.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffcc00" />
<embed src="/swf/movie.swf" quality="high" bgcolor="#ffcc00" width="1" height="1" name="MyMovie" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
When I am calling the external function like this:
swf.MyExternalFunction();
In IE9 i get a JS error:
"Object doesn't support property or method 'MyExternalFunction'"
Apparently window[movieName], which was good for IE, does not work in IE9 the way it used to. Any suggestions?
:::UPDATE:::
Here is my solution so far. It may not look pretty, but it works:
var movie = false;
function initSWF(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
//alert("IE");
if (typeof (window[movieName].MyExternalFunction) == 'function') {
// alert("< IE9");
movie = window[movieName];
}
else if (typeof (document[movieName].MyExternalFunction) == 'function') {
// alert(">= IE9");
movie = document[movieName];
}
}
else {
// alert("NON IE");
movie = document[movieName];
}
return ((movie) ? true : false);
}
Then it is used like:
function flashReady() // This is called from ActionScript
{
if(initSwf("MyMovie")) {
开发者_Go百科 movie.MyExternalFunction();
} else {
alert("Failed to initialize");
}
}
I've had this exact same problem with IE9, for me, it was the case of IE9 caching the flash. Try clearing the browser cache (f12 for developer tools and there's an icon to clear the cache or ctrl-R i think), and then give it a shot again.
If that doesnt, try debugging by delaying your javascript call to actionscript by 1 or 2 seconds, something along the lines of this :
<SCRIPT LANGUAGE='Javascript'>
function delayForFlash() {
setTimeout("startFlash()", 1000);
}
function startFlash() {
getFlashMovie("flashdemo").restartFlash();
}
window.onload = function(){ delayForFlash();}
</SCRIPT>
This is to give enough time for your flash to load everything up.
I've been having similar trouble except that it was working in IE9 and not IE7/8. I believe the cause of both our problems are the same though. I solved it by stumbling across this wonderful article on A List: Apart http://www.alistapart.com/articles/flashsatay/
Following the article, your correctly formed HTML should read:
<object type="application/x-shockwave-flash" data="/swf/movie.swf" width="1" height="1" id="MyMovie">
<param name="allowScriptAccess" value="always" />
<param name="movie" value="/swf/movie.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffcc00" />
</object>
In particular notice that I've added a type
and removed the <embed>
tag.
As far as I can see, there is no need to use the <embed>
tag any more. And removing it means we can use much simpler code to fire our ExternalInterface function:
// After the flash object has loaded...
var movie = document.getElementById('MyMovie');
if (typeof movie.MyExternalFunction === 'function') movie.MyExternalFunction();
I hope this helps.
精彩评论