Marcelo Noronha
Marcelo Noronha

Reputation: 815

Communication between javascript and Flash

I´ve already added:

ExternalInterface.addCallback('sendToActionScript', setKeyboardFocus);
ExternalInterface.call("setFocus", 'pathfinder');

inside the init() function of my main class.

In html I have this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    <title>pathFinder</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css" media="screen">
    html, body { height:100%; background-color: #333333;}
    body { margin:0; padding:0; overflow:hidden; }
    #flashContent { width:100%; height:100%; }
    </style>
 <script type="text/javascript">



 function getFlashMovie(movieName) {
 var isIE = navigator.appName.indexOf("Microsoft") != -1;
 return (isIE) ? window[movieName] : document[movieName];
 }



function callToActionscript(str) 
{  

var fm = getFlashMovie("pathfinder");

fm.sendToActionScript(str);
}

function setFocus(id){ 

 var f =  document.getElementById(id);
 f.focus();
 callToActionscript('test') 

 } 


</script>
</head>
<body>



    <div id="flashContent" align='center'>
<table width="100%" height="100%" border="0" align="center" cellpadding="0"  
cellspacing="0">
<tr>
  <td align="center" valign="middle" bgcolor="#333333"><table width="1050" border="0"       
cellpadding="0" cellspacing="0">

    <tr>
      <td>
    <div align="center">
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#versio
 n=6,0,0,0" 
WIDTH="1050" 
HEIGHT="600" 
id="pathfinder" 
ALIGN="middle">
<PARAM NAME=movie VALUE="pathFinder.swf"> 
<PARAM NAME=quality VALUE=high> 
<PARAM NAME=bgcolor VALUE=#333333> 
<EMBED src="pathFinder.swf" quality=high bgcolor=#333333 WIDTH="1050" HEIGHT="600" 
NAME="pathfinder" ALIGN="middle" TYPE="application/x-shockwave-flash" 
PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" allowScriptAccess="always"> 
</EMBED> </OBJECT></div></td></tr></td></tr></table>

    </div>
</body></html>

In the main stage I have a dynamic text field instance 'test_txt' to check if the function is called.

So after the ExternalInterface code I have:

private function setKeyboardFocus(str:String):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN,checkKeysDown);
stage.addEventListener(KeyboardEvent.KEY_UP,checkKeysUp)
test_txt.text = str
}

The problem is flash doesn´t get keyboard focus ( the event listeneres of KeyboardEvent are never added ), the function setKeyboardFocus is never called.

Any help?

Upvotes: 0

Views: 808

Answers (2)

shaunhusain
shaunhusain

Reputation: 19748

Verify that fm is being assigned to the object or embed component. You should be able to verify this using the javascript debugging tools in Chrome or in Firefox. I think it's here where you're going wrong. This is a completely non-standard html wrapper from what I've seen myself but for the most part it appears you have things in order. One thing that is going to be wrong though is that the Object tag and information will be applied for IE, the Embed tag information will be applied for browsers that use the Netscape plugin (Firefox... well everything but IE). Also I'm not seeing an id on the Embed element, I think you need to also give this an ID like you did with the Object, I'm not sure if you'll get javascript errors if you use the same exact ID, I would probably call it pathFinderE or something like that then modify this method:

 function getFlashMovie(movieName) {
 var isIE = navigator.appName.indexOf("Microsoft") != -1;
 return (isIE) ? window[movieName] : document[movieName + "E"];
 }

Upvotes: 1

Sam DeHaan
Sam DeHaan

Reputation: 10325

According to one source I found, you need to add allowScriptAccess="always" in two places.

  1. Where you have it, in the <EMBED ... allowScriptAccess="always"> </EMBED> block.
  2. You also need <PARAM NAME="allowScriptAccess" VALUE="always" > with the other PARAM blocks

Upvotes: 1

Related Questions