简体   繁体   English

javascript和Flash之间的通信

[英]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. 在我的主类的init()函数内。

In html I have this: 在HTML中我有这个:

<!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. 在主阶段,我有一个动态文本字段实例'test_txt'来检查函数是否被调用。

So after the ExternalInterface code I have: 所以在ExternalInterface代码之后我有:

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. 问题是flash没有得到键盘焦点(从不添加KeyboardEvent的事件监听),从不调用函数setKeyboardFocus。

Any help? 有帮助吗?

According to one source I found, you need to add allowScriptAccess="always" in two places. 根据我发现的一个来源,你需要在两个地方添加allowScriptAccess="always"

  1. Where you have it, in the <EMBED ... allowScriptAccess="always"> </EMBED> block. 你有它的地方,在<EMBED ... allowScriptAccess="always"> </EMBED>块中。
  2. You also need <PARAM NAME="allowScriptAccess" VALUE="always" > with the other PARAM blocks 您还需要<PARAM NAME="allowScriptAccess" VALUE="always" >与其他PARAM块

Verify that fm is being assigned to the object or embed component. 验证是否已将fm分配给对象或嵌入组件。 You should be able to verify this using the javascript debugging tools in Chrome or in Firefox. 您应该可以使用Chrome或Firefox中的javascript调试工具对此进行验证。 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. 这是一个完全非标准的html包装,来自我自己看到的,但在大多数情况下,它看起来你的东西是有序的。 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). 那将是错误的,虽然有一件事是,对象标签和信息将适用于IE浏览器,嵌入标签信息将被应用为使用Netscape浏览器插件(火狐......很好,但一切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: 另外我没有看到嵌入元素ID,我想你也需要给这一个ID喜欢你的对象做了,我不知道,如果你使用完全相同的ID,你会得到的JavaScript错误,我可能会称之为pathFinderE或类似的东西,然后修改此方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM