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 />

Once the runtimeApplicationDomain property is set, the JavaScript context shares class definitions with the<br />

assigned domain. To create an instance of a custom class in JavaScript, refer<strong>en</strong>ce the class definition through the<br />

window.runtime property and use the new operator:<br />

var customClassObject = new window.runtime.CustomClass();<br />

The HTML cont<strong>en</strong>t must be from a compatible security domain. If the HTML cont<strong>en</strong>t is from a differ<strong>en</strong>t security<br />

domain than that of the application domain you assign, the page uses a default application domain instead. For<br />

example, if you load a remote page from the Internet, you could not assign ApplicationDomain.curr<strong>en</strong>tDomain as the<br />

application domain of the page.<br />

Removing ev<strong>en</strong>t list<strong>en</strong>ers<br />

Adobe AIR 1.0 and later<br />

Wh<strong>en</strong> you add JavaScript ev<strong>en</strong>t list<strong>en</strong>ers to objects outside the curr<strong>en</strong>t page, including runtime objects, objects in<br />

loaded SWF cont<strong>en</strong>t, and ev<strong>en</strong> JavaScript objects running in other pages, you should always remove those ev<strong>en</strong>t<br />

list<strong>en</strong>ers wh<strong>en</strong> the page unloads. Otherwise, the ev<strong>en</strong>t list<strong>en</strong>er dispatches the ev<strong>en</strong>t to a handler function that no longer<br />

exists. If this happ<strong>en</strong>s, you will see the following error message: “The application attempted to refer<strong>en</strong>ce a JavaScript<br />

object in an HTML page that is no longer loaded." Removing unneeded ev<strong>en</strong>t list<strong>en</strong>ers also lets AIR reclaim the<br />

associated memory. For more information, see “Removing ev<strong>en</strong>t list<strong>en</strong>ers in HTML pages that navigate” on page 1022.<br />

Accessing HTML DOM and JavaScript objects from<br />

ActionScript<br />

Adobe AIR 1.0 and later<br />

Once the HTMLLoader object dispatches the complete ev<strong>en</strong>t, you can access all the objects in the HTML DOM<br />

(docum<strong>en</strong>t object model) for the page. Accessible objects include display elem<strong>en</strong>ts (such as div and p objects in the<br />

page) as well as JavaScript variables and functions. The complete ev<strong>en</strong>t corresponds to the JavaScript page load ev<strong>en</strong>t.<br />

Before complete is dispatched, DOM elem<strong>en</strong>ts, variables, and functions may not have be<strong>en</strong> parsed or created. If<br />

possible, wait for the complete ev<strong>en</strong>t before accessing the HTML DOM.<br />

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

<br />

<br />

foo = 333;<br />

function test() {<br />

return "OK.";<br />

}<br />

<br />

<br />

Hi.<br />

<br />

<br />

This simple HTML page defines a JavaScript variable named foo and a JavaScript function named test(). Both of these<br />

are properties of the global 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), which you can access using the getElem<strong>en</strong>tById() method. Once the page is loaded (wh<strong>en</strong> the<br />

HTMLLoader object dispatches the complete ev<strong>en</strong>t), you can access each of these objects from ActionScript, as shown<br />

in the following ActionScript code:<br />

Last updated 6/6/2012<br />

988

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

Saved successfully!

Ooh no, something went wrong!