這段時間一直在苦心研究Flex,今天忽然想,咱們平時都是把swf放到網頁中,怎麼才能把網頁嵌入到Flex中呢?我查了一些資料,而後通過本身的不懈努力,終於搞定。
爲了方便,寫了個嵌入HTML頁面的代理IFrame組件,該組件封裝了全部須要的Flex端代碼,後面只要經過標籤調用就OK了。
IFrame.mxml文件以下:html
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" resize="callLater(moveIFrame)" move="callLater(moveIFrame)" initialize="init();">
- <mx:Script>
- <![CDATA[
- import mx.controls.Alert;
- import flash.external.ExternalInterface;
- import flash.geom.Point;
- import flash.net.navigateToURL;
- private var __source: String;
-
-
-
-
- public function moveIFrame():void
- {
- var localPoint:Point = new Point(0, 0);
- var globalPoint:Point = this.localToGlobal(localPoint);
- ExternalInterface.call("moveIFrame", globalPoint.x, globalPoint.y, this.width, this.height);
- }
-
-
- public function set source(source: String): void {
- if (source)
- {
- if (!ExternalInterface.available)
- {
- Alert.show("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 init():void
- {
- ExternalInterface.addCallback("IFrameOnBulr",showIFrameAgain);
- }
-
-
- public function showIFrameAgain():void
- {
- this.source=this.__source;
- this.showIFrame=true;
- this.moveIFrame();
- }
-
-
- public function set showIFrame(display:Boolean):void
- {
- ExternalInterface.call("IFrameShow",display);
- }
-
- 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>
- IFremaDemo.mxml文件以下:
-
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:zq="*">
- <zq:IFrame mouseUp="Iframe.showIFrame=true;Iframe.moveIFrame();Iframe.source=Iframe.source" id="Iframe" source="http://weather.news.qq.com/inc/ss258.htm" x="240" y="23" width="190" height="190"/>
- </mx:Application>
固然少不了js代碼,IFremaDemo.html網頁是Flex Builder3自動生成的,而後須要加上如下代碼:java
- <script>
- function moveIFrame(x,y,w,h) {
- var frameRef=document.getElementById("myFrame");
- frameRef.style.left=x;
- frameRef.style.top=y;
- }
- function loadIFrame(url){
- document.getElementById("myFrame").src=url;
- }
-
- function IFrameShow(display){
- document.getElementById("myFrame").style.visibility=display?"visible":"hidden";
- }
-
- function IFrameOnBulr()
- {
-
- var flash = (navigator.appName.indexOf ("Microsoft") !=-1)?window["IFremaDemo"]:document["IFremaDemo"];
-
- flash.IFrameOnBulr();
- }
- </script>
- <iframe id="myFrame" name="myFrame" onblur="this.style.visibility='visible';IFrameOnBulr();" width="189" height="190" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" style="position:absolute;"></iframe>
轉自:http://www.cnblogs.com/YNLDY/archive/2012/02/07/2340968.html瀏覽器