How to create Flash movie that call external javascript function using SWF Scout SDK

Submitted by eLenka on 6 July, 2011 - 16:02

Flash player control that is used to play flash movies in Internet Explorer, Mozilla and other web browsers provides a way to communicate with Javascript and Flash. To call Javascript function from a Flash movie you should:

a) set ID parameter of Flash player object tag to the some name (e.g. "myflashmovie")
b) set NAME parameter of Flash player embed tag to the same name ("myflashmovie")
c) declare Javascript function in HEAD tag section with name. Name of this function should begin with the name of your flash movie + DoFSCommand (in our example it is myflashmovie_DoFSCommand )
Thus the declaration of Javascript function must be like this: function myflashmovie_DoFSCommand(command, args) {}
This Javascript function will be executed then flash movie will call FSCommand

d) Create a flash movie that will call FSCommand with command and argument parameters that will be passed to the Javascript function

Below is a sample flash movie SampleFlash.swf that calls the javascript function when you click a rounded button:

 

VBScript code that creates SampleFlash.swf movie using SWF Scout SDK:

W = 640
 
H = 480
 
Set Movie = CreateObject("SWFScout.FlashMovie")
 
Movie.InitLibrary "demo","demo"
 
' Movie parameters

Movie.BeginMovie 0,0,W,H,1,12,6
 
Movie.Compressed = true
 
Movie.SetBackgroundColor 255,255,255
 
Font = Movie.AddFont( "Arial",12,true,false,false,false,0)
 
Text= Movie.AddTextEdit ("mtext","Click button below to call external javascript function",155,0,0,255,Font,30,80,300,200)
 
Movie.TEXT_ReadOnly= false
 
Movie.TEXT_NoSelect=false
 
Movie.TEXT_Align = 0' staLeft align 

Movie.TEXT_Multiline = true
 
Movie.TEXT_WordWrap = true
 
Movie.PlaceText Text,Movie.CurrentMaxDepth
 
Shape = Movie.AddShape
 
Movie.SHAPE_Circle 0, 0, 50
 
Movie.SHAPE_BeginRadialGradient
 
Movie.SHAPE_AddRadialGradientColor 255,255,255,255
 
Movie.SHAPE_AddRadialGradientColor 102,102,102,255
 
Movie.SHAPE_EndRadialGradient 35,35
 
Button = Movie.AddButton (false,true)
 
Movie.BUTTON_AddShape Shape,0
 
Movie.BUTTON_AddShape Shape,1
 
Movie.BUTTON_AddShape Shape,2
 
Movie.BUTTON_AddShape Shape,3
 
Action= Movie.AddScript
 
Movie.SCRIPT_GetUrl "FSCommand:call_my_alert", "Hello from flash movie!"
 
Movie.BUTTON_SetScriptOnEvent 3,Action ' set action on button Release

Movie.PlaceButton Button,Movie.CurrentMaxDepth
 
Movie.PLACE_SetTranslate 70,250
 
Movie.ShowFrame 1
 
Movie.EndMovie
 
Movie.SaveToFile "SampleFlash.swf"


The source code of ViewFlashWithJavaScript.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
 < HTML>
 <HEAD>
 <SCRIPT LANGUAGE="JavaScript">
 < !--
 // function should start with myflashmovie_ prefix as we set ID=myflashmovie in object tag and Name="myflashmovie" // in Embed tag below
 function myflashmovie_DoFSCommand(command, args) {
 if (command == "call_my_alert") {
 alert("Message from flash movie: " + args);
 }
 }
 //-->
 < /SCRIPT>
 <SCRIPT LANGUAGE="VBScript">
 < !--
 // VB script catches FSCommand too
 sub myflashmovie_FSCommand(ByVal command, ByVal args)
 call myflashmovie_DoFSCommand(command, args)
 end sub
 //-->
 < /SCRIPT>
 < /HEAD>
 
<BODY>
 < OBJECT
 CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
 WIDTH="100%"
 HEIGHT="100%"
 CODEBASE="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"
 ID=myflashmovie VIEWASTEXT>
 <PARAM NAME="MOVIE" VALUE="SampleFlash.swf">
 <PARAM NAME="PLAY" VALUE="false">
 <PARAM NAME="LOOP" VALUE="false">
 <PARAM NAME="QUALITY" VALUE="high">
 <PARAM NAME="SCALE" VALUE="SHOWALL">
 
<EMBED
 NAME="myflashmovie"
 SRC="SampleFlash.swf"
 WIDTH="100%"
 HEIGHT="100%"
 PLAY="false"
 LOOP="false"
 QUALITY="high"
 SCALE="SHOWALL"
 swLiveConnect="true"
 PLUGINSPAGE="http://www.macromedia.com/go/flashplayer/">
 </EMBED>
 < /OBJECT>
 < /BODY>
 < /HTML>

 

Download source code: