Home > Web Front-end > JS Tutorial > body text

JS access to SWF function usage examples_javascript skills

WBOY
Release: 2016-05-16 15:51:52
Original
947 people have browsed it

The example in this article describes the function usage of JS to access SWF. Share it with everyone for your reference. The specific analysis is as follows:

There have been many examples of Flash and JS calling each other. This is not a problem. Of course, calling errors is not an error between Flash and JS. Today we will completely solve the incompatibility problem between IE, FF and Chrome. !

We know that in Flash, if you access external JS functions, you only need to

Copy the code The code is as follows:
flash .external.ExternalInterface.call("Function name in JS");
This sentence is enough

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

import flash.external.*;
var methodName:String = "SetImgPath";
//JS需要调用的函数名字
var instance:Object = null;
var method:Function = extractstr;
//Flash中实际的函数名字,参数忽略,但是你调用的时候要记得有参数的要加上
var wasSuccessful:Boolean = ExternalInterface.addCallback(methodName, instance, method);
trace(wasSuccessful+"-callback")

Copy after login

We can trace it. If it is true at this time, it means the registration is successful

OK, try it out on the web below:

<div style="margin-left:50px; margin-bottom:50px"><input type="text" id="mytxt" border="1" value="这是测试框,调用页面JS演示" 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"><!--下面针对非IE内核浏览器--> 
     <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="获得 Adobe Flash Player" />
     </a>
    <!--[if !IE]>-->
    </object>
    <!--<![endif]-->
   </object>
  </div>

Copy after login

OK, we add a button to the Web to call this flashcall:

Test it:

Everything works fine for IE6, 7, 8, 9

FF: Failed

Chrome: Failed

Cause of failure: SetImgPath is not a function /not defined)

This is very strange, we can use an alert to output

Copy code The code is as follows:
thisMovie ("123"): alert(thisMovie("123"));

The output result is not Null, which means our Flash has been found, but why can’t FF and Chrome always find the function?

After browsing various forums, the consensus is that addCallback must be loaded with flash before the registration can be successful. Well, let’s add a settimeout function to determine whether the loading is successful. Let’s change the above flashcall:

function flashcall(str){  
     try{   
  thisMovie("123").SetImgPath(str);//注意,123是上面的ID,而SetImgPath是我们Flash中的注册的回调函数
 }cache(e){
  settimeout("flashcall(str)",100 );
       }
}

Copy after login

Well, we use a settimeout and call it every 100 milliseconds. If there is an exception, we will continue to call until it succeeds!

Unfortunately, it never succeeded and the program entered an infinite loop!

After consulting various information, I finally found the reason:

The browser in FF only recognizes the embed tag, so if you use getElementById to obtain flash, you need to tag the embed with an ID. IE recognizes the object tag, so you need to tag the ID on the object.

Do you understand? It turns out that in FF and Chrome, Flash must use embed to identify its ID, and the Object tag cannot identify the ID. The Html I use is automatically generated by Flash, and it is all Object tags, which is harmful. After debugging for an afternoon, I realized that the problem lies here.

Now that you know the reason, it will be easier 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="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>

Copy after login

Change the tag embedded in Flash to the one above, and everything is OK!

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!