HTML與AIR之間的溝通
還有重要的官方注腳:
The JavaScript environment has its own Document and Window objects. JavaScript code can interact with the AIR run-time environment through the runtime, nativeWindow, and htmlLoader properties. ActionScript code can interact with the JavaScript environment through the window property of an HTMLLoader object, which is a reference to the JavaScript Window object. In addition, both ActionScript and JavaScript objects can listen for events disptached by both AIR and JavaScript objects.
例子: (在JS中讓AIR的native window發出事件)
JavaScript:
-
//似乎不能用自定事件, 可惜可惜,
-
//不然就可以直接利用事件處理把資料傳遞到AIR runtime了
-
var e = new air.Event("foobar");
-
window.nativeWindow.dispatchEvent( e );
例子2: ( 在AIR中監聽 Javascript DOM 事件)
Actionscript:
-
//只能監聽DOM預設事件//
-
HTML#htmlLoader.window.document.addEventListener("click" , onClick );
例子3: ( sandbox bridge , 取得不同sandbox的資料)
JavaScript:
-
//在child html定義bridge
-
var interface = {};
-
interface.foobar = "blahblah....";
-
window.childSandboxBridge = interface;
JavaScript:
-
//parent html裡頭, 取得定義好的bridge
-
var childInterface = {};
-
childInterface = document.getElementById("myDIV").contentWindow.childSandboxBridge;
-
air.trace( childInterface.foobar )
-
//as console output: blahblah...//
Tags: AIR, javascript

