flash javascript之间的通讯方法小结

不用getURL和fsCommand方法

  flash使用的actionscript跟javascript是非常相通的,下面描述如何互相调用函数:

  1:javascript调用flash中的函数

  在flash的脚本中增加

  import flash.external.ExternalInterface;

  假定要调用的函数是hello,as代码如下

  function hello(){

  return "hello";

  }

  ExternalInterface.addCallback("hello", this, hello);

  //第一个参数为导出函数名,第三个参数为as的函数名,这样就可以在js中调用as的hello函数了

  2:flash调用js的函数

  ExternalInterface.call("hello2", "jacky");

  //第一个参数是js的函数名,后面的是js函数的参数

  3:如何互相调用

  html代码如下:

  <object type="application/x-shockwave-flash" data="test.swf" width="525" height="390" name="test">

  <param name="allowScriptAccess" value="sameDomain" />

  <param name="movie" value="test.swf" />

  <param name="quality" value="high" />

  <param name="scale" value="noScale" />

  <param name="wmode" value="transparent" />

  </object>

  javascript代码如下:

  function callFromFlash() {

  var a=thisMovie("test").hello();

  alert(a);

  }

  function thisMovie(movieName) {

  if (navigator.appName.indexOf("Microsoft") != -1) {

  return window[movieName]

  }

  else {

  return document[movieName]

  }

  }

  //注意,不能使用document.getElementById此类函数取得网页中的flash对象,只能使用thisMovie函数中的代码

  国外看到的另一种方法:

  You can't call a function, but you can change/set a variable and use the watch() method to execute the code whenever the value is changed.

  ActionScript Code:

  function changeType(prop, oldval, newval) {

  //do your stuff

  return newval;

  }

  var strType = "";

  this.watch("strType", changeType);