在開發過程中Flex 與 JavaScript 相互調用是在所難免,在國內很少有單純的Flex Web應用。
Flex內建了ExternalInterface提供與Javascript的互操作。示例程序爲ExternalInterface的應用,還包括了彈出窗口是否被攔截的判斷。
文檔可以在下面的地址查看:
[url]http://livedocs.adobe.com/flex/3/langref/index.html[/url]
[url=http://myflex.googlecode.com/svn/trunk/flexTips/bin-debug/DetectPopupBlocked.html]查看示例[/url]
[url=http://code.google.com/p/myflex/source/browse/#svn/trunk/flexTips]查看示例源碼[/url]
將Flex方法暴露給JavaScript
src/DetectPopupBlocked.mxml
[Bindable]
private var greeting:String="What?";
private function init():void{
if(ExternalInterface.available){
ExternalInterface.addCallback("saySomething", saySomething);
}
}
public function saySomething(_greeting:String):void{
trace(_greeting);
greeting = _greeting;
}
JavaScript調用Flex方法
html-template/index.template.html
function saySomethingToFlex(bla){
${application}.saySomething(bla);
}
Flex調用Javascript 函數,彈出窗口,並且判斷彈出窗口是否被攔截。
src/DetectPopupBlocked.mxml
private function openNewBrowserWindow(url:String, name:String):void{
if(ExternalInterface.available){
var opened:Boolean = ExternalInterface.call("openPopup", url, name);
if(!opened){
Alert.show("Popup Blocked!");
}
}
}
html-template/index.template.html
function openPopup(url, name){
var popwindow;
popwindow = window.open(url, name, "menubar=false,resizable=false,width=790,height=500");
// detect popup blocked
if (popwindow==null || typeof(popwindow)=="undefined"){
return false;
}
return true;
}
-------------------------------------
It's Neal Mi. I'm a man of my world.