JavaScript access to SWF functions compatible with IE, FF, and Chrome

Source: Internet
Author: User

If you want to JS access to the functions in flash, you need to register a callback function with Addcallback, let JS to call (AS2 example)

The code is as follows Copy Code

Import flash.external.*;
var methodname:string = "Setimgpath";//js the name of the function that needs to be called
var instance:object = null;
var method:function = The actual function name in Extractstr;//flash, parameter ignored, but when you call, remember that there are parameters to add
var Wassuccessful:boolean = Externalinterface.addcallback (methodname, instance, method);
Trace (wassuccessful+ "-callback")

We can trace it, this time if it's true, it means registration is successful

OK, try this in the Web below:

The code is as follows Copy Code

<div style= "margin-left:50px; margin-bottom:50px "><input type=" text "id=" Mytxt "border=" 1 "value=" This is the test box, call page JS demo "style=" width:200px "/>" </div>
<div id= "Flashcontent" >
<object classid= "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width= "515" "height=" 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= "" ><!--below for non IE kernel browsers >
<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= "Get Adobe Flash Player"/>
</a>
<!--[if! Ie]>-->
</object>
<!--<! [endif]-->
</object>
</div>

This side of the web code we use Adobe Flash automatically generated, and then write the corresponding JS code:

  code is as follows copy code

<script
   function Flashcall (str) {   
     Thismovie ("123"). Setimgpath (str);/Note that 123 is the ID above, and Setimgpath is the registered callback function in our flash <BR>
    }
     function Thismovie (moviename) {//This is the universal search for flash functions. IE is object, and FF is document
   var Isie = navigator.appName.indexOf ("Microsoft")!=-1;
    return (Isie)? Window[moviename]: Document[moviename];
  }
</script> 

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

Test:

ie6,7,8,9, everything's fine.

FF: Failed

Chrome: Failed

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

This is strange, we can use an alert to output Thismovie ("123"): Alert (Thismovie ("123"));

The output is not NULL, which means that we have found our flash, but why are FF and chrome always unable to find the function?

Visited the major forums, the more consistent saying is that addcallback must be a flash loading completed to register successfully, well, we add a settimeout function to determine whether the loading success, we change the above flashcall:

The code is as follows Copy Code

function Flashcall (str) {
try{
Thismovie ("123"). Setimgpath (str);/Note that 123 is the ID above, and Setimgpath is the registered callback function in our Flash
}cache (e) {
SetTimeout ("Flashcall (str)", 100);
}
}

Well, we use a settimeout, every 100 milliseconds to call once, if the exception continues to call, until the success!

Unfortunately, always unsuccessful, the program into the dead loop!

In the crash

By looking at the various materials, we finally found the reason:

FF browser only know embed tag, so if you use getElementById to get flash, you need to give embed ID tag, and IE is aware of object tags, so you need the ID on the object to do your mark

Do you understand? Originally in the FF and Chrome, Flash must use embed to identify his ID, with the OBJECT tag is not recognized ID, I use the HTML is flash automatically generated, are object tags, I debug a afternoon, just understand the original problem out here.

Know the reason, it is good to deal with, change the label:

The code is as follows Copy Code

 < 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=" 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=" ></EMBED>
</object>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.