在flex組件中嵌入html代碼,能夠利用flex iframe。這個在不少時候會用到的,有時候flex必須得這樣作,若是你不這樣作還真不行……javascript
flex並且能夠和html進行JavaScript交互操做,flex調用到html中的JavaScript方法以及獲取調用後的返回值。html
一、flex iframe下載地址:https://github.com/downloads/flex-users/flex-iframe/flex-iframe-1.5.1.zipjava
下載完成後目錄以下git
asdoc就是文檔doc了github
bin有須要用到的flex庫 swctomcat
examples就是示例服務器
sources源代碼app
歡迎關注個人博客:http://hoojo.cnblogs.com函數
二、將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請求的頁面,本地靜態頁面是沒法顯示的。