JavaScript如何调用调用flash中的方法

  本文内容主要介绍了当在flash中注册了一个方法提供给js调用时,不同浏览器如何调用此方法的异同。
  flash中注册提供给JavaScript调用的方法如下所示
+展开
-ActionScript
    function hello(){getURL("javascript:alert('你好!')");}
    flash.external.ExternalInterface.addCallback("hello",null,hello);

  上面只是一个简单的示例,在flash中调用js内置函数alert输出“你好”信息,然后注册此方法名也为hello,当然你可以指定其他的提供给js调用的方法,具体flash.external.ExternalInterface.addCallback如何使用自己查找flash的帮助文档。

  网页测试代码如下
+展开
-HTML
<body><p align="center">
<object width="400" height="200" type="application/x-shockwave-flash" id="playerIE">
<param value="hello.swf" name="movie"/>
</object>

<embed  type="application/x-shockwave-flash" id="playerFF" src="hello.swf"></embed>
</p></body>
<script type="text/javascript">
window.onerror=function(e){alert(e);return true;}
window.onload=function(){
  
  alert("IE测试:\n\n"+(window['playerIE']&&(typeof window['playerIE'].hello))+'\n\n'
      +(document['playerIE']&&(typeof document['playerIE'].hello))+'\n\n'
      +(typeof document.getElementById('playerIE').hello)
      +'\n\n\n'
      +"W3C浏览器:\n\n"+(window['playerFF']&&(typeof window['playerFF'].hello))+'\n\n'
      +(document['playerFF']&&(typeof document['playerFF'].hello))+'\n\n'
      +(typeof document.getElementById('playerFF').hello))
}
</script> 

  下面分别是IE6,FF2,opera9,google4,和safari3的输入出结果

IE中调用flash方法


firefox中调用flash方法

opera中调用flash方法

google浏览器中调用flash方法

safari中调用flash方法


  从输出中可以看出
  1)FF,opera要调用flash注册的方法,得需要embed对象加载flash。FF支持document,document.getElementById 2种方法。Opera支持window,document,document.getElementById 3种方法
  2)IE只能使用object对象来加载flash。并且支持window,document,getElementById的方法来获取object对象的引用。
  3)safari和google浏览器视乎不支持调用flash中注册方法,具体方法不知道如何解决。


  对于FF,给object加上data属性
+展开
-HTML
<object width="400" height="200" type="application/x-shockwave-flash" data="hello.swf" id="playerIE">

后也可以使用object对象加载flash,并且支持document,document.getElementById 2种方法获取flash对象的引用。如下

firefox中使用调用flash注册方法


  注意:object加上data属性只对Firefox有效果,opera,safari,google浏览器都不行。

加支付宝好友偷能量挖...


原创文章,转载请注明出处:JavaScript如何调用调用flash中的方法

评论(0)Web开发网
阅读(193)喜欢(0)JavaScript/Ajax开发技巧