13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Programming HTML and JavaScript in AIR<br />

var libraryObject = new window.runtime.utilities.LibraryClass();<br />

Note: To compile an ActionScript SWF library for use as part of an HTML page in AIR, use the acompc compiler. The<br />

acompc utility is part of the Flex SDK and is described in the Flex SDK docum<strong>en</strong>tation.<br />

Accessing the HTML DOM and JavaScript objects from an imported<br />

ActionScript file<br />

Adobe AIR 1.0 and later<br />

To access objects in an HTML page from ActionScript in a SWF file imported into the page using the tag,<br />

pass a refer<strong>en</strong>ce to a JavaScript object, such as window or docum<strong>en</strong>t, to a function defined in the ActionScript code.<br />

Use the refer<strong>en</strong>ce within the function to access the JavaScript object (or other objects accessible through the passed-in<br />

refer<strong>en</strong>ce).<br />

For example, consider the following HTML page:<br />

<br />

<br />

<br />

num = 254;<br />

function getStatus() {<br />

return "OK.";<br />

}<br />

function runASFunction(window){<br />

var obj = new runtime.ASClass();<br />

obj.accessDOM(window);<br />

}<br />

<br />

<br />

Body text.<br />

<br />

<br />

This simple HTML page has a JavaScript variable named num and a JavaScript function named getStatus(). Both of<br />

these are properties of the window object of the page. Also, the window.docum<strong>en</strong>t object includes a named P elem<strong>en</strong>t<br />

(with the ID p1).<br />

The page loads an ActionScript file, “ASLibrary.swf,” that contains a class, ASClass. ASClass defines a function named<br />

accessDOM() that simply traces the values of these JavaScript objects. The accessDOM() method takes the JavaScript<br />

Window object as an argum<strong>en</strong>t. Using this Window refer<strong>en</strong>ce, it can access other objects in the page including<br />

variables, functions, and DOM elem<strong>en</strong>ts as illustrated in the following definition:<br />

public class ASClass{<br />

public function accessDOM(window:*):void {<br />

trace(window.num); // 254<br />

trace(window.docum<strong>en</strong>t.getElem<strong>en</strong>tById("p1").innerHTML); // Body text..<br />

trace(window.getStatus()); // OK.<br />

}<br />

}<br />

You can both get and set properties of the HTML page from an imported ActionScript class. For example, the<br />

following function sets the cont<strong>en</strong>ts of the p1 elem<strong>en</strong>t on the page and it sets the value of the foo JavaScript variable<br />

on the page:<br />

Last updated 6/6/2012<br />

993

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!