在Flex(Flash)中嵌入HTML代码或页面(Flex IFrame)
在flex组件中嵌入html代码,可以利用flex iframe。这个在很多时候会用到的,有时候flex必须得这样做,如果你不这样做还真不行……
flex而且可以和html进行javascript交互操作,flex调用到html中的javascript方法以及获取调用后的返回值。
1、flex iframe下载地址:https://github.com/downloads/flex-users/flex-iframe/flex-iframe-1.5.1.zip
下载完成后目录如下
asdoc就是文档doc了
bin有需要用到的flex库 swc
examples就是示例
sources源代码
2、将bin目录中的swc引入到你的flex工程中,并加入代码片段如下
<?xml version="1.0" encoding="utf-8"?>
<mx:application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:flexiframe="http://code.google.com/p/flex-iframe/"
horizontalalign="center" verticalalign="middle" xmlns:s="library://ns.adobe.com/flex/spark">
<mx:script>
<![cdata[
import mx.controls.alert;
protected function sayhellohandler(event:mouseevent):void {
// 调用当前iframe嵌入页面中的sayhello 的javascript方法
iframebysource.calliframefunction("sayhello");
}
protected function sayhandler(event:mouseevent):void {
// 调用当前iframe嵌入页面中的say的javascript方法,并传入一个参数
iframebysource.calliframefunction("say", ["hello world!"]);
}
protected function sayhihandler(event:mouseevent):void {
// 调用当前iframe嵌入页面中的sayhi的javascript方法,并传入2个参数。sayhi方法会返回一个字符串,最后一个回调就是输出值的函数
iframebysource.calliframefunction("sayhi", ["hello world", "李四"], function (data:*): void {
alert.show(data);
});
}
]]>
</mx:script>
<!-- html content stored in a string -->
<mx:string id="iframehtmlcontent">
<![cdata[
<html>
<head>
<title>about</title>
</head>
<body>
<div>about</div>
<p>simple html test application. this test app loads a page of html locally.</p>
<div>credits</div>
<p> </p>
<p>iframe.as is based on the work of</p>
<ul>
<li><a href="http://coenraets.org/" target="_top">christophe coenraets</a></li>
<li><a href="http://www.deitte.com/" target="_top">brian deitte</a></li>
</ul>
</body>
</html>
]]>
</mx:string>
<mx:panel width="80%" height="80%" title="使用source本地远程页面">
<flexiframe:iframe id="iframebysource" width="100%" height="100%" source="frame.html"/>
<s:button label="sayhello" click="sayhellohandler(event)"/>
<s:button label="say-param" click="sayhandler(event)"/>
<s:button label="sayhi" click="sayhihandler(event)"/>
</mx:panel>
<mx:panel width="80%" height="80%" title="使用source加载远程页面">
<flexiframe:iframe id="iframebyremotesource" width="100%" height="100%" source="http://www.baidu.com" visible="true"
overlaydetection="true" />
</mx:panel>
<mx:panel width="80%" height="80%" title="使用content属性 加载本地html文本内容">
<flexiframe:iframe id="iframebycontent" width="100%" height="100%" content="{iframehtmlcontent}"/>
</mx:panel>
</mx:application>
frame.html 页面内容
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>frame.html</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<script type="text/javascript">
// 无参数
function sayhello() {
alert("hello......");
}
// 1个参数
function say(message) {
alert("your say: " + message);
}
// 多个参数 并返回值
function sayhi(message, name) {
alert("your say: " + message + ", name: " + name);
return "your say: " + message + ", name: " + name;
}
</script>
</head>
<body>
flex frame example html page!
<input type="button" value="say" onclick="sayhello()"/>
</body>
</html>
要注意的是:你的flex项目工程需要发表到http的应用服务器(如tomcat、jboss、iis)这些服务器中,用http请求方式才能调用到页面内容和javascript方法。如果不发布到应用服务器中,那样只能在iframe中嵌套远程的http请求的页面,本地静态页面是无法显示的。
如对本文有疑问,
点击进行留言回复!!
网友评论