使用ExternalInterface调用Flex方法-传参数、有返回值!
2013-01-28 17:07
423 查看
生成flash的mxml文件源码如下:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="app(event)" >
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
private function app(event:FlexEvent):void{
ExternalInterface.addCallback("flex_hello_world",hello_world);
}
private function hello_world(msg:String,str:String):String{
return msg+"-"+str;
}
]]>
</fx:Script>
</s:Application>
插入flash的html文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>使用ExternalInterface调用Flex方法-传参数、有返回值!</title>
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
function js_hello_world(){
var obj=document.getElementById('flash');
var msg=obj.flex_hello_world("使用ExternalInterface调用Flex方法","传参数、有返回值");
alert(msg);
}
function js_hello_world2(){
var obj=document.getElementById('flash2');
var msg=obj.flex_hello_world("使用ExternalInterface调用Flex方法","传参数、有返回值");
alert(msg);
}
function js_hello_world3(){
var obj=document.getElementById('flash3');
var msg=obj.flex_hello_world("使用ExternalInterface调用Flex方法","传参数、有返回值");
alert(msg);
}
</script>
</head>
<body>
<input type="button"value="swfobject" width="100"
<div id="flash"></div>
<script type="text/javascript">
swfobject.embedSWF("js4.swf", "flash", "400", "325", "11.1.0", "playerProductInstall.swf", {});
</script>
<input type="button"value="swfobject" width="100"
<embed src="js4.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="400" height="325" id="flash2"></embed>
<input type="button"value="swfobject" width="100"
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="400" height="325" id="flash3">
<param name="movie" value="js4.swf" />
<param name="quality" value="high" />
</object>
</body>
</html>
注:object方式ie以外无法调用Flex方法,embed方式ie无法调用Flex方法
参考:http://blog.csdn.net/xuhuanchao/article/details/4900859
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="app(event)" >
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
private function app(event:FlexEvent):void{
ExternalInterface.addCallback("flex_hello_world",hello_world);
}
private function hello_world(msg:String,str:String):String{
return msg+"-"+str;
}
]]>
</fx:Script>
</s:Application>
插入flash的html文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>使用ExternalInterface调用Flex方法-传参数、有返回值!</title>
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
function js_hello_world(){
var obj=document.getElementById('flash');
var msg=obj.flex_hello_world("使用ExternalInterface调用Flex方法","传参数、有返回值");
alert(msg);
}
function js_hello_world2(){
var obj=document.getElementById('flash2');
var msg=obj.flex_hello_world("使用ExternalInterface调用Flex方法","传参数、有返回值");
alert(msg);
}
function js_hello_world3(){
var obj=document.getElementById('flash3');
var msg=obj.flex_hello_world("使用ExternalInterface调用Flex方法","传参数、有返回值");
alert(msg);
}
</script>
</head>
<body>
<input type="button"value="swfobject" width="100"
<div id="flash"></div>
<script type="text/javascript">
swfobject.embedSWF("js4.swf", "flash", "400", "325", "11.1.0", "playerProductInstall.swf", {});
</script>
<input type="button"value="swfobject" width="100"
<embed src="js4.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="400" height="325" id="flash2"></embed>
<input type="button"value="swfobject" width="100"
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="400" height="325" id="flash3">
<param name="movie" value="js4.swf" />
<param name="quality" value="high" />
</object>
</body>
</html>
注:object方式ie以外无法调用Flex方法,embed方式ie无法调用Flex方法
参考:http://blog.csdn.net/xuhuanchao/article/details/4900859
相关文章推荐
- 使用ExternalInterface调用Flex方法-无参数、无返回值!
- 使用ExternalInterface调用JS方法-无参数、无返回值!
- android调用webservice方法,参数或返回值使用服务器端类
- 使用ExternalInterface调用JS方法-传参数、有返回值!
- studio警告: 最后一个参数使用了不准确的变量类型的 varargs 方法的非 varargs 调用
- 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试
- 使用 HttpWebRequest的psot方式传参数调用接口方法
- 使用Btrace来动态监控方法的参数和返回值
- Java 带参数带返回值方法的使用
- atitit.架构设计---方法调用结果使用异常还是返回值
- 动态类型调用方法的参数和返回值
- 使用<a>标签调用struts2的action、传递动态参数、以及参数中出现加号等特殊字符的处理方法
- 调用存储过程并且使用返回值的基本方法
- 使用extjs,来实现一个页面调用另一个页面中的方法,并且能获得方法的返回值
- atitit.架构设计---方法调用结果使用异常还是返回值
- Error:(95, 74) 警告: 最后一个参数使用了不准确的变量类型的 varargs 方法的非 varargs 调用; 对于 varargs 调用, 应使用 Class<?> 对于非 varar
- 结构变量作为方法的参数调用,在方法内部使用的“坑”你遇到过吗?
- 调用对方WSDL中方法,使用CALL方式传递字符串参数
- JavaDay03--Notes(数组的遍历和部分拷贝+方法的调用、参数和返回值)
- Flex调用js方法传递参数