JS accesses an instance of SWF's function usage

  • 2020-06-22 23:54:12
  • OfStack

This article gives an example of how JS accesses SWF's functions. Share to everybody for everybody reference. The specific analysis is as follows:

Much has been said about the example of Flash and JS calling each other, this is not a problem, of course, this is not the error of Flash and JS calling, we will solve the incompatibility problem between IE, FF and Chrome once and for all today!

We know that in Flash, if you access the external JS function, you just need to

flash.external.ExternalInterface.call("JS Function name in ");
This one sentence is enough

If you want JS to access a function in Flash, you need to register a callback function with addCallBack for JS to call (example AS2)


import flash.external.*;
var methodName:String = "SetImgPath";
//JS The name of the function to call 
var instance:Object = null;
var method:Function = extractstr;
//Flash The actual name of the function is ignored, but remember to add the parameters when you call it 
var wasSuccessful:Boolean = ExternalInterface.addCallback(methodName, instance, method);
trace(wasSuccessful+"-callback")

We can use trace1. At this time, if it is true, it means the registration is successful

OK, try 1 in web below:


<div style="margin-left:50px; margin-bottom:50px"><input type="text" id="mytxt" border="1" value=" This is the test box, the call page JS demo " style="width:200px"/></div>
  <div id="flashContent">
   <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="515" height="320" id="123" align="middle">
    <param name="movie" value="123.swf" />
    <param name="quality" value="high" />
    <param name="bgcolor" value="#000000" />
    <param name="play" value="true" />
    <param name="loop" value="true" />
    <param name="wmode" value="transparent" />
    <param name="scale" value="showall" />
    <param name="menu" value="true" />
    <param name="devicefont" value="false" />
    <param name="salign" value="" />
    <param name="allowScriptAccess" value="sameDomain" />
    <!--[if !IE]>-->
    <object type="application/x-shockwave-flash" data="123.swf" width="515" height="320"><!-- The following is for non- IE Kernel browser --> 
     <param name="movie" value="123.swf" />
     <param name="quality" value="high" />
     <param name="bgcolor" value="#000000" />
     <param name="play" value="true" />
     <param name="loop" value="true" />
     <param name="wmode" value="transparent" />
     <param name="scale" value="showall" />
     <param name="menu" value="true" />
     <param name="devicefont" value="false" />
     <param name="salign" value="" />
     <param name="allowScriptAccess" value="sameDomain" />
    <!--<![endif]-->
     <a href="upload/2011/1/201101281000491420.gif" alt=" To obtain  Adobe Flash Player" />
     </a>
    <!--[if !IE]>-->
    </object>
    <!--<![endif]-->
   </object>
  </div>

OK, we add an button to Web to call this flashcall: < input type="button" onclick="flashcall('./image/ 1.jpg ')" value="JS call SWF" >

Test 1:

IE6, 7,8,91 cut is normal

FF: failure

Chrome: failure

Reason for failure: SetImgPath is not a function, (is not a function /not defined)

So that's kind of weird, we can use 1 alert for the output

thisMovie("123") : alert(thisMovie("123"));

The output is not Null, indicating that our Flash has been found, but why FF and Chrome cannot always find the function.

After browsing various forums, it is said by comparison of 1 that addCallback must load flash before it can be registered successfully. Ok, we add 1 settimeout function to determine whether the load is successful. We change 1 to flashcall above:


function flashcall(str){  
&nbsp;&nbsp;&nbsp;&nbsp; try{   
  thisMovie("123").SetImgPath(str);// Pay attention to, 123 Is the above ID And the SetImgPath It is our Flash Is a registered callback function 
 }cache(e){
  settimeout("flashcall(str)",100 );
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
}

Well, we use settimeout, which calls every 100 milliseconds, and if the exception keeps calling until it succeeds!

Unfortunately, it didn't work out and the program went into an endless loop!

Through looking up various materials, I finally found the reason:

In FF, the browser only knows the embed tag, so if you get flash using getElementById, you need to tag ID for embed, and IE knows the object tag, so you need to tag ID for object

Do you understand? It turned out that in FF and Chrome, Flash had to use embed to identify his ID. ID could not be identified with Object tag. The Html I used was automatically generated by Flash, both of which were Object tags.

Once you know the reason, it's easy to deal with it. Change the label:


<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="515" height="320" id="123" title="123" align="middle">
 <param name="allowScriptAccess" value="always" />
 <param name="movie" value="123.swf">
 <param name="quality" value="high">
  <param name="wmode" value="transparent" />
 <embed src="123.swf" name="123" quality="high" allowScriptAccess="always" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="515" height="320"></embed>
</object>

Change the tag embedded in Flash to above, 1 cut OK!

I hope this article has been helpful for your javascript programming.


Related articles: