JS Access SWF's function usage Example _javascript skill

Source: Internet
Author: User

This article illustrates the function usage of JS accessing SWF. Share to everyone for your reference. The specific analysis is as follows:

About Flash and JS call each other example has been said a lot, this is not a problem, of course, call error This is not flash and JS error, we today to completely solve the IE,FF and chrome between the incompatibility problem!

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

Copy Code code as follows:
Flash.external.ExternalInterface.call ("Function name in JS");
This sentence can be

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)

Import flash.external.*;
var methodname:string = "Setimgpath";
JS needs to call the function name
var instance:object = null;
var method:function = extractstr;
Flash in the actual function name, parameters ignored, but you call to 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:

<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" 515 "height=" id= "123" align= "middle" > <param name= "movie" value= "123.swf"/> <param name= "Quality" "Value="/> <param name= "bgcolor" value= "#000000"/> <param "Play" Name= "true" value= & Lt;param name= "Loop" value= "true"/> <param name= "wmode" value= "Transparent"/> <param name= "scale" va Lue= "ShowAll"/> <param name= "menu" value= "true"/> "<param" name= "Devicefont" value= "false"/> & Lt;param name= "salign" value= "/> <param name=" allowscriptaccess value= "Samedomain"/> <!--[if! <object type= "Application/x-shockwave-flash" Data= "123.swf" width= "515" height= "ie]>-->" ><!-- Below for non IE kernel browsingDevice--> <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 "/> <p Aram name= "Menu" value= "true"/> <param name= "Devicefont" value= "false"/> "<param name=" salign "Valu" E= "" "/> <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>

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

It's strange that we can use an alert to output

Copy Code code as follows:
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:

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!

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 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, To understand the original problem is here.

Know the reason, it is good to deal with, 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=" 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>

Change the label of the embedded flash to above, all ok!

I hope this article will help you with your JavaScript programming.

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.