18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Chapter 18<br />

default. If you are using the element, even for backwards compatibility, you must add a special<br />

attribute called swLiveConnect:<br />

<br />

<br />

<br />

<br />

<br />

<br />

With the attribute set, you can be assured that the Flash-to-<strong>JavaScript</strong> communication channel is open.<br />

Flash provides two different ways to achieve this interaction: getURL() and fsCommand(). Both can<br />

send only primitive values to <strong>JavaScript</strong>, and each has its own strengths and weaknesses.<br />

getURL()<br />

The getURL() function is a generic way of interacting with the browser. It can be used to open up a<br />

document in the browser window (or in a new window) similar to window.open() is <strong>JavaScript</strong>. <strong>For</strong><br />

example, you can open www.wrox.com in a new browser window with the following code:<br />

getURL(“http://www.wrox.com”, “_blank”);<br />

Because getURL() simply passed the given URL to the browser, it can accept javascript: URLs as<br />

well. <strong>For</strong> example, suppose you have a function, getMessageFromFlash(), that accepts a string as its<br />

only argument and then displays that string in an alert, such as in the following:<br />

function getMessageFromFlash(sMessage) {<br />

alert(“Flash says: \”” + sMessage + “\”.”);<br />

}<br />

Inside of the Flash movie, create a button and assign the following ActionScript to it:<br />

on(release) {<br />

getURL(“javascript:getMessageFromFlash(\”Hello from Flash!\”)”);<br />

}<br />

When you export the movie and embed it in the HTML page containing getMessageFromFlash(),<br />

clicking on the button pops up the <strong>JavaScript</strong> alert displaying the text “Hello from Flash!”<br />

The getURL() function is the simplest way to call <strong>JavaScript</strong> from Flash, although you have another<br />

way to do this.<br />

fscommand()<br />

Using fscommand() in Flash is like sending a message to <strong>JavaScript</strong>. This message consists of a command<br />

(indicating the action the movie is expecting) and a single argument (although Flash allows you to<br />

enter more than one argument, this isn’t handled correctly by <strong>JavaScript</strong>). A typical call from inside a<br />

Flash movie looks like this:<br />

556

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

Saved successfully!

Ooh no, something went wrong!