ActionScript 3 接收 JavaScript函數傳回值

來源:互聯網
上載者:User
MXML:<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" 
    creationComplete="onCreationComplete()">
    
    <mx:Script source="ExternalInterfaceASJSReturn.as" />
    
    <mx:Panel height="300" width="500" title="ExternalInterface : Returning a value to ActionScript from JavaScript">
        <mx:Canvas height="100%" width="100%">
            <mx:Button y="6" label="Get Browser Info" id="submitButton" width="118" x="180">
            </mx:Button>
            <mx:TextArea x="5" y="34" width="468" height="228" id="tArea"/>
        </mx:Canvas>
    </mx:Panel>
</mx:Application>

 
ExternalInterfaceASJSReturn.as:
import flash.external.ExternalInterface;
import flash.events.MouseEvent;


private function onCreationComplete():void
...{
    submitButton.addEventListener("click", onSubmitClick);
}

private function onSubmitClick(event:MouseEvent):void
...{
    if(ExternalInterface.available)
    ...{
        //call the JavaScript function, and store the return value
        //in a variable
        var info:Object = ExternalInterface.call("getBrowserInfo");
        
        var sb:String = null;
        
        //loop through the results
        for(var x:String in info)
        ...{
            sb = sb + x + " : " + info[x] + " ";
        }

        //print them out to the TextArea
        tArea.text = sb.toString();
    }
}

JavaScript:function getBrowserInfo()
    ...{
        var docElement = document.documentElement;

        var o = new Object();
        
            o.href = location.href;
            o.lang = docElement.lang;
            o.offsetTop = docElement.offsetTop;
            o.offsetLeft = docElement.offsetLeft;
            o.offsetWidth = docElement.offsetWidth;
            o.offsetHeight = docElement.offsetHeight;
            o.scrollTop = docElement.scrollTop;
            o.scrollLeft = docElement.scrollLeft;
            o.scrollHeight = docElement.scrollHeight;
            o.scrollWidth = docElement.scrollWidth;
            o.clientHeight = docElement.clientHeight;
            o.clientWidth = docElement.clientWidth;
            o.width = document.width;
            o.height = document.height;
            o.domain = document.domain;
            o.lastModified = document.lastModified;        
                
        return o;
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.