<!DOCTYPE HTML><HTML> <Head> <title>Swf</title> <MetaCharSet= "Utf-8"/> <styletype= "Text/css">html, Body{Height:100%;Background-color:Antiquewhite;}Body{margin:0;padding:0;Overflow:Hidden; } </style> </Head> <Body> <DivID= "Flashcontent"> <!--embed is for non-IE, where the name of embed is very important and must match the ID - <ObjectClassID= "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"width= "550"Height= "The "ID= "SWF"> <paramname= "movie"value= "swf.swf" /> <paramname= "Wmode"value= "Window" /> <paramname= "allowScriptAccess"value= "All" /> <Embedsrc= "swf.swf"width= "550"Height= "The "name= "SWF"Wmode= "Window"allowscriptaccess= "All"type= "Application/x-shockwave-flash"pluginspage= "Http://www.macromedia.com/go/getflashplayer"> </Object> </Div> <Div> <textareaID= "txt"cols= "+"rows= "Ten"></textarea> <ButtonID= "BTN">JS Call as</Button> </Div> <Scripttype= "Text/javascript"> vartxt=document.getElementById ("txt"); varbtn=document.getElementById ("btn"); varSWF=window["SWF"] ||document["SWF"]; Btn.onclick= function() {swf["Jscallas"]("I ' m js."); //Jscallas is a function of addcallback in Flash . } functionAscalljs (s) {Txt.value+=s+ "\ n"; } </Script> </Body></HTML>
The above is the HTML code, the following is the as code
Import flash.events.MouseEvent; Externalinterface.addcallback ("Jscallas", Onjscallashandler); function Onjscallashandler (s:string):void { tf.appendtext ("as Received:""\ N ");} Btn.addeventlistener (Mouseevent.click, onascalljshandler); function Onascalljshandler (e:mouseevent):void { Externalinterface.call ( "Ascalljs" "hi! I ' m as. " );}
As we all know, the most commonly used method of nested flash is OE (object-embed) mode, in addition to some embedding methods, you can refer to the article in Kissyui http://docs.kissyui.com/1.4/docs/html/ Tutorials/kissy/swf/embed-swf-onto-webpage.html
This example only considers OE mode, other embedded types are self-testing, this article but more discussion.
This example is tested in Ie6-8 and modern browsers by example using the most standard OE way nested flash, the principle is ie get object, non-IE get embed,
The key point in HTML is that the name of the embed must match the ID of the object, otherwise the chrome downgrade does not use embed, and the SWF is empty, so the browser will report an error with no Jscallas method:
Other properties of the flash tag in HTML: Wmode this does not affect the call in this write is to facilitate everyone to copy the code, after all, this property is very common
allowScriptAccess not never can not write this property or flash will not be able to communicate with JS
As in the code is relatively simple, do not need too much skill, reference AS3 API can, mainly Externalinterface call and Addcallback method.
Online Demo Address: Http://gotoandlearn.net/asjs
JS and Flash (AS) call each other