flex獲得html容器傳遞的url參數值

來源:互聯網
上載者:User

 

我們經常在Flex程式需要用從外部html向swf檔案傳遞參數,(類似 test.html?name=jex&address=chengdu 地址中問號後面的參數對值)

首先要明確的是,一般我們在使用Flex Builder進行Flex開發時,編譯後自動以html容器將swf檔案封裝起來了,所以一般來說,我們直接啟動並執行是html,而非直接運行產生的swf檔案。而Flex應用程式要擷取外部html容器傳入的參數,通常是用JavaScript來擷取到相應參數,再讓javaScript傳遞給ActionScript。

在Flex應用程式中,我們通常要用到ExternalInterface類,ExternalInterface主要用來讓ActionScript直接與Flash Player容器進行通訊。ExernalInterface類通常作為ActionScript與JavaScript進行通訊的橋樑。

為了擷取從html傳入的URL參數,通常傳遞的順序是:html容器—>JavaScript—>ExternalInterface—>ActionScript

 

具體實現:
在Flex中,通過調用ExternalInterface的call方法,參數為要調用的JavaScript函數,並返回JS函數調用的結果。如:

  1. ExternalInterface.call("JavaScript函數");  
ExternalInterface.call("JavaScript函數");

 

在JS中,Window對象用來代表一個Web瀏覽器視窗,而視窗的Location對象則代表了當前顯示的URL,於是,要想擷取URL中的參數,

通常使用下面的語句:

 

  1. window.location.href.toString  //得到URL的完整文本   
  2.     
  3. window.location.search.substring  //得到問號後面部分的URL文本  
window.location.href.toString  //得到URL的完整文本 window.location.search.substring  //得到問號後面部分的URL文本

 

:這裡window屬性引用的Window對象自身,而Window對象的location屬性引用的是Location對象。

 

通常的參數對以test.html?name=jex&address=chengdu 這樣的形式給出,在擷取到問號後面的URL文本後,還需要對其分解,這時有兩種途徑,一種是分解過程在JS中完成,然後將最終的結果值傳遞給Flex,另一種是將分解的過程放在Flex中去完成。在這裡使用的後者(這樣只需寫AS代碼,而不用去寫JS代碼了^_^)

           代碼如下:

           <?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
    creationComplete="init()"> 
<mx:Script> 
    <![CDATA[ 
        import mx.controls.Alert; 
  
        private var params:Object; 
  
        private function init():void { 
            btnID.addEventListener(MouseEvent.CLICK, clickHandler); 
        } 
  
        private function clickHandler(evt:Event):void { 
            var args:Object = getParams(); 
            if(args.name != null && args.address != null) { 
                dispID.text = "name:" + args.name + "/n" + "address:" + args.address; 
            } 
        } 
  
        private function getParams():Object { 
            params = {}; 
            var query:String = ExternalInterface.call("window.location.search.substring", 1); 
            // Alert.show(ExternalInterface.call("window.location.href.toString",1)); 
            // Alert.show(query); 
            if(query) { 
                var pairs:Array = query.split("&"); 
                for(var i:uint=0; i < pairs.length; i++) { 
                    var pos:int = pairs[i].indexOf("="); 
                    //Alert.show(String(pos)); 
                    if(pos != -1) { 
                        var argname:String = pairs[i].substring(0, pos); 
                        var value:String = pairs[i].substring(pos+1); 
  
                        params[argname] = value; 
                    } 
                } 
            } 
            return params; 
        } 
    ]]> 
</mx:Script> 
    <mx:Button id="btnID" y="118" label="GetParams" horizontalCenter="0"/> 
    <mx:TextArea id="dispID" y="47" width="200" horizontalCenter="0"/>  
</mx:Application> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.