↑
Main Page
getURL
default. If you are using the
<embed/>
element, even for backwards compatibility, you must add a spe-
cial attribute called
swLiveConnect
:
<object type=”application/x-shockwave-flash” data=”myflashmovie.swf”
width=”100” height=”100”>
<param name=”message” value=”Hello World! “ />
<embed type=”application/x-shockwave-flash” src=”myflashmovie.swf”
width=”100” height=”100” swLiveConnect=”true”>
<param name=”message” value=”Hello World!” />
</embed>
</object>
With the attribute set, you can be assured that the Flash-to-JavaScript communication channel is open.
Flash provides two different ways to achieve this interaction:
getURL()
and
fsCommand()
. Both can
send only primitive values to JavaScript, and each has its own strengths and weaknesses.
getURL()
The
getURL()
function is a generic way of interacting with the browser. It can be used to open up a
document in the browser window (or in a new window) similar to
window.open()
is JavaScript. For
example, you can open
www.wrox.com
in a new browser window with the following code:
getURL(“http://www.wrox.com”, “_blank”);
Because
getURL()
simply passed the given URL to the browser, it can accept
javascript:
URLs as
well. For example, suppose you have a function,
getMessageFromFlash()
, that accepts a string as its
only argument and then displays that string in an alert, such as in the following:
function getMessageFromFlash(sMessage) {
alert(“Flash says: \”” + sMessage + “\”.”);
}
Inside of the Flash movie, create a button and assign the following ActionScript to it:
on(release) {
getURL(“javascript:getMessageFromFlash(\”Hello from Flash!\”)”);
}
When you export the movie and embed it in the HTML page containing
getMessageFromFlash()
,
clicking on the button pops up the JavaScript alert displaying the text
“Hello from Flash!”
The
getURL()
function is the simplest way to call JavaScript from Flash, although you have another
way to do this.
fscommand()
Using
fscommand()
in Flash is like sending a message to JavaScript. This message consists of a com-
mand (indicating the action the movie is expecting) and a single argument (although Flash allows you to
enter more than one argument, this isn’t handled correctly by JavaScript). A typical call from inside a
Flash movie looks like this:
556
Chapter 18
21_579088 ch18.qxd 3/28/05 11:43 AM Page 556
Free JavaScript Editor
Ajax Editor
©
→