我们如何在flex中使用addCallback函数

时间:2012-04-18 12:25:38

标签: javascript flex

如何从java脚本调用flex函数? 我正在使用下面链接中定义的代码

ExternalInterface.addCallback( "javascriptfunction", flexfunction);

http://www.switchonthecode.com/tutorials/flex-javascript-basics-using-externalinterface

http://circlecube.com/2010/12/actionscript-as3-javascript-call-flash-to-and-from-javascript/

2 个答案:

答案 0 :(得分:0)

您应该为您的问题提供更多背景信息。根据我在教程中阅读的内容,您可能省略了对代码添加安全性访问权限。更准确地说,请看这里:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html#addCallback%28%29,尤其是:

  

SecurityError - 包含的环境属于调用代码无权访问的安全沙箱。若要解决此问题,请按照下列步骤操作:     在包含HTML页面中SWF文件的object标记中,设置以下参数:

param name="allowScriptAccess" value="always" 

In the SWF file, add the following ActionScript:

flash.system.Security.allowDomain(sourceDomain)

希望它有所帮助。

答案 1 :(得分:0)

flex 4.5

首先添加一个addcallback,就像在你的mxml

中一样
public function initApp():void {
        ExternalInterface.addCallback("myFlexFunction",myFunc);
}  
现在可以从您的javascript访问

myFlexFunction

让你的index.template.html看起来像这样

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <title>addCallback() Wrapper</title>
        <script type="text/javascript" src="swfobject.js"></script>
        <script type="text/javascript">
            var swfVersionStr = "0";
            var xiSwfUrlStr = "";
            var flashvars = {};
            var params = {};
            params.quality = "high";
            params.bgcolor = "#ffffff";
            params.allowscriptaccess = "sameDomain";
            var attributes = {};
            attributes.id = "AddCallbackExample";
            attributes.name = "AddCallbackExample";
            attributes.align = "middle";
            swfobject.embedSWF(
                "AddCallbackExample.swf", "flashContent",
                "100%", "100%",
                swfVersionStr, xiSwfUrlStr,
                flashvars, params, attributes);
        </script>
    </head>
    <SCRIPT LANGUAGE="JavaScript">
        function callApp() {
            window.document.title = document.getElementById("newTitle").value;
            var AddCallbackExample = document.getElementById("AddCallbackExample");
            AddCallbackExample.myFlexFunction(window.document.title);
        }
    </SCRIPT>

    <body>
        <form id="f1">
            Enter a new title: <input type="text" size="30" id="newTitle" onchange="callApp()">
        </form>
        <div id="flashContent"/>
   </body>
</html>

和你的flex应用程序一样

<?xml version="1.0"?>
<!-- wrapper/AddCallbackExample.mxml -->
<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:mx="library://ns.adobe.com/flex/mx" 
    xmlns:s="library://ns.adobe.com/flex/spark" creationComplete="initApp()">
    <fx:Script>
        import flash.external.*;

         public function initApp():void {
            ExternalInterface.addCallback("myFlexFunction",myFunc);
         }  

         public function myFunc(s:String):void {
            l1.text = s;
         }
    </fx:Script>

    <s:Label id="l1"/>

</s:Application>

希望这有帮助