在Flex中用Iframe嵌入HTML网页

本文详细介绍了在Flex应用中嵌入HTML代码的三种方法:使用Flex文本组件的htmlText属性、将Flex应用嵌入HTML页面并通过ExternalInterface进行交互、以及使用HTML的Iframe标签导入HTML页面。此外,还提供了一个自定义的IFrame组件,简化了嵌入过程。

有时候我们需要在Flex应用中嵌入HTML代码,根据嵌入HTML要求的不同有以下三种方法:

1、Flex文本组件(Label、Text、TextArea)的htmlText属性支持一些基本的HTML代码,例如:

  1. <mx:TextArea>   
  2. <mx:htmlText>   
  3.     <!--[CDATA[   
  4.       <p align="center"><font size="15" color="#3399ff">this is a html code</font></p>   
  5.     ]]-->   
  6. </mx:htmlText>   
  7. </mx:TextArea>  
<mx:TextArea> <mx:htmlText> <!--[CDATA[ <p align="center"><font size="15" color="#3399ff">this is a html code</font></p> ]]--> </mx:htmlText> </mx:TextArea> 

2、我们可以将Flex应用嵌入到HTML页面中,然后通过Flex2中的ExternalInterface(Flex1.5使 getURL("javascript.:javascriptMethod"))
    来实现Flex与HTML javascript的相互交互,进一步的,如果要在Flex应用中嵌入完整的HTML呢? 
    其实实现的方法很简单,只需要使用HTML的Iframe标签来导入需嵌入的HTML页面,
    然后使用ExternalInterface调用相应的javasript将该Iframe移动到我们Flex页面需要嵌入HTML页面的部分之上就可以 了,示意图如下:

    也就是说,我们包含Flex SWF文件的HTML页面主要有三个部分:
1、Flex swf 插件容器,FlexBuilder自动生成部分

  1. <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"   
  2.     id="IFrameDemo" width="100%" height="100%"    
  3.     codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">    
  4.       <param name="movie" value="IFrameDemo.swf" />   
  5.       <param name="quality" value="high" />   
  6.       <param name="bgcolor" value="#869ca 7" />   
  7.       <embed src="IFrameDemo.swf" mce_src="IFrameDemo.swf" quality="high" bgcolor="#869ca7"    
  8.         width="100%" height="100%" name="detectiontest" aligh="middle"    
  9.         play="true" loop="false"   quality="high"     
  10.         allowScriptAccess="sameDomain"    
  11.         type="application/x-shockwave-flash"    
  12.         wmode="opaque"   
  13.         swLiveConnect="true"   
  14.         pluginspage="http://www.macromedia.com/go/getflashplayer">   
  15.       </embed>   
  16.    </object>  
  17.     2、HTML Iframe标 签,绝对定位,用来导入HTML页面  
  18. <iframe id="myFrame" name="myFrame"   frameborder="0"   
  19.    style="position:absolute;background-color:transparent;border:0px;visibility:hidden;" mce_style="position:absolute;background-color:transparent;border:0px;visibility:hidden;" />  
  20.     3、移动Iframe和在Iframe 中导入需嵌入FLEX中的HTML页面的脚本  
  21. <mce:script type="text/javascript"><!--  
  22.    
  23. function moveIFrame(x,y,w,h) {   
  24.     var frameRef=document.getElementById("myFrame");   
  25.     frameRef.style.left=x;   
  26.     frameRef.style.top=y;   
  27.     frameRef.width=w;   
  28.     frameRef.height=h;   
  29. }   
  30. function loadIFrame(url){   
  31.    top.frames["myFrame"].location.href=url;   
  32. }   
  33. // --></mce:script>  
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="IFrameDemo" width="100%" height="100%" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"> <param name="movie" value="IFrameDemo.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#869ca 7" /> <embed src="IFrameDemo.swf" mce_src="IFrameDemo.swf" quality="high" bgcolor="#869ca7" width="100%" height="100%" name="detectiontest" aligh="middle" play="true" loop="false" quality="high" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" wmode="opaque" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer"> </embed> </object> 2、HTML Iframe标签,绝对定位,用来导入HTML页面 <iframe id="myFrame" name="myFrame" frameborder="0" style="position:absolute;background-color:transparent;border:0px;visibility:hidden;" mce_style="position:absolute;background-color:transparent;border:0px;visibility:hidden;" /> 3、移动Iframe和在Iframe中导入需嵌入FLEX中的HTML页面的脚本 <mce:script type="text/javascript"><!-- function moveIFrame(x,y,w,h) { var frameRef=document.getElementById("myFrame"); frameRef.style.left=x; frameRef.style.top=y; frameRef.width=w; frameRef.height=h; } function loadIFrame(url){ top.frames["myFrame"].location.href=url; } // --></mce:script> 

Flex中的导入Iframe页面和移动Iframe的代码如下:

  1. import flash.external.ExternalInterface;   
  2. import flash.geom.Point;   
  3. import flash.net.navigateToURL;   
  4. private var __source: String;   
  5. private function moveIFrame(): void {   
  6.     var localPt:Point = new Point(0, 0);   
  7.     var globalPt:Point = this.localToGlobal(localPt);   
  8.     ExternalInterface.call("moveIFrame", globalPt.x, globalPt.y, this.width, this.height);   
  9. }   
  10. public function set source(source: String): void {   
  11.     if (source) {   
  12.         if (! ExternalInterface.available)   
  13.         {   
  14.          // TODO: determine if this Error is actually needed.   The debugger    
  15.          // build gives the error below.   Assuming that this error will not show   
  16.          // up in the release build but haven’t checked.   
  17.             throw new Error("The ExternalInterface is not available in this container. Internet Explorer ActiveX,  
  18.      Firefox, Mozilla 1.7.5 and greater, or other browsers that support NPRuntime are required.");   
  19.         }   
  20.         __source = source;   
  21.         ExternalInterface.call("loadIFrame", source);   
  22.     }   
  23. }  
import flash.external.ExternalInterface; import flash.geom.Point; import flash.net.navigateToURL; private var __source: String; private function moveIFrame(): void { var localPt:Point = new Point(0, 0); var globalPt:Point = this.localToGlobal(localPt); ExternalInterface.call("moveIFrame", globalPt.x, globalPt.y, this.width, this.height); } public function set source(source: String): void { if (source) { if (! ExternalInterface.available) { // TODO: determine if this Error is actually needed. The debugger // build gives the error below. Assuming that this error will not show // up in the release build but haven’t checked. throw new Error("The ExternalInterface is not available in this container. Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater, or other browsers that support NPRuntime are required."); } __source = source; ExternalInterface.call("loadIFrame", source); } } 

    两个方法分别直接调用使用ExternalInterface.call调用前面我们提到的HTML页面上的两个Javascript方法。另外一个要注 意的是<Canvas/>
继承自flash.display.DisplayObject类的localToGlobal方法的使 用,该方法将基于Flash场景的坐标转换为基于全局本地坐标,也就是浏览器页面坐标:

//Flash场景0,0坐标 var localPt:Point = new Point(0, 0); //转换为浏览器页面坐标 var globalPt:Point = this.localToGlobal(localPt); 
这 样就可以在Flex页面中嵌入任意的HTML页面了,为了方便,Brian写了个嵌入HTML页面的代理IFrame组件,该组件封装了所有需要的 Flex端代码: 
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <mx:Canvas xmlns:mx="http://www.macromedia.com/2005/mxml"   
  3.     resize="callLater(moveIFrame)"   
  4.     move="callLater(moveIFrame)">   
  5.     <mx:Script>   
  6.     <!--[CDATA[   
  7.         import flash.external.ExternalInterface;   
  8.         import flash.geom.Point;   
  9.         import flash.net.navigateToURL;   
  10.         private var __source: String;   
  11.         private function moveIFrame(): void {   
  12.            var localPt:Point = new Point(0, 0);   
  13.             var globalPt:Point = this.localToGlobal(localPt);   
  14.             ExternalInterface.call("moveIFrame", globalPt.x, globalPt.y, this.width, this.height);   
  15.         }   
  16.         public function set source(source: String): void {   
  17.             if (source) {   
  18.                if (! ExternalInterface.available)   
  19.                 {   
  20.                  // TODO: determine if this Error is actually needed.   The debugger    
  21.                  // build gives the error below.   Assuming that this error will not show   
  22.                  // up in the release build but haven’t checked.   
  23.                     throw new Error("The ExternalInterface is not available in this container. Internet Explorer ActiveX, Firefox,  
  24.       Mozilla 1.7.5 and greater, or other browsers that support NPRuntime are required.");   
  25.                 }   
  26.                 __source = source;   
  27.                 ExternalInterface.call("loadIFrame", source);   
  28.             }   
  29.         }   
  30.         public function get source(): String {   
  31.             return __source;   
  32.         }   
  33.         override public function set visible(visible: Boolean): void {   
  34.             super.visible=visible;   
  35.             if (visible)   
  36.             {   
  37.                 ExternalInterface.call("showIFrame");   
  38.             }   
  39.             else   
  40.             {   
  41.                 ExternalInterface.call("hideIFrame");   
  42.             }   
  43.         }   
  44.     ]]-->   
  45.     </mx:Script>   
  46. </mx:Canvas>  
<?xml version="1.0" encoding="utf-8"?> <mx:Canvas xmlns:mx="http://www.macromedia.com/2005/mxml" resize="callLater(moveIFrame)" move="callLater(moveIFrame)"> <mx:Script> <!--[CDATA[ import flash.external.ExternalInterface; import flash.geom.Point; import flash.net.navigateToURL; private var __source: String; private function moveIFrame(): void { var localPt:Point = new Point(0, 0); var globalPt:Point = this.localToGlobal(localPt); ExternalInterface.call("moveIFrame", globalPt.x, globalPt.y, this.width, this.height); } public function set source(source: String): void { if (source) { if (! ExternalInterface.available) { // TODO: determine if this Error is actually needed. The debugger // build gives the error below. Assuming that this error will not show // up in the release build but haven’t checked. throw new Error("The ExternalInterface is not available in this container. Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater, or other browsers that support NPRuntime are required."); } __source = source; ExternalInterface.call("loadIFrame", source); } } public function get source(): String { return __source; } override public function set visible(visible: Boolean): void { super.visible=visible; if (visible) { ExternalInterface.call("showIFrame"); } else { ExternalInterface.call("hideIFrame"); } } ]]--> </mx:Script> </mx:Canvas> 

    该IFrame组件有个source属性用来记录需要载入的嵌入HTML页面的地址,每次source属性更新时,调用 ExternalInterface.call("loadIFrame", source)
调用javascript方法loadIFrame 方法在HTML 页面中的IFrame中载入要嵌入的HTML页面。
另外,重载了Canvas的visible属性,以便在Canvas隐藏 HTML页面中的IFrame。
如下使用该组件在Flex应用中嵌入HTML页面方法:

  1. <IFrame id="iFrame" source="http://blog.eshangrao.com" width="300" height="400"   />  
<IFrame id="iFrame" source="http://blog.eshangrao.com" width="300" height="400" /> 

以上代码将在我们的Flex应用中嵌入本站首页。

(3) 前面的两种方法都是用的Flex2.0的老的函数,如果觉得麻烦的话也可以使用现成的类

地址:http://code.google.com/p/flex-iframe/

将类文件加入项目,加下面的语句在需要的地方,指明URL就好了

  1. <local:IFrame id="iFrame" source="http://www.google.com" width="100%" height="100%" />  
<local:IFrame id="iFrame" source="http://www.google.com" width="100%" height="100%" /> 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值