如何让 javascript 与 flash 通信(Object #<HTMLObjectElement> 没有方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14345936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:24:07  来源:igfitidea点击:

how to allow javascript to communicate with flash (Object #<HTMLObjectElement> has no method

javascriptactionscript-3flashswfobjectexternalinterface

提问by mheavers

I'm trying to send a simple test message from javascript to flash, but I'm getting the error:

我正在尝试从 javascript 向 flash 发送一条简单的测试消息,但出现错误:

Object #<HTMLObjectElement> has no method "listenToJS"

I've read a number of questions on this on stack, but I feel like either the browser is not getting the proper reference to my flash object, or within my actionscript I am not putting my flash function in the proper place.

我已经在堆栈上阅读了许多关于此的问题,但我觉得浏览器没有正确引用我的 flash 对象,或者在我的 actionscript 中我没有将我的 flash 功能放在正确的位置。

So within html I am embedding flash with SWFObj:

所以在 html 中,我用 SWFObj 嵌入了 flash:

<div id="flash_content">
    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="1280" height="800" id="tourFlash" name="pano" class="pano">
        <param name="movie" value="VRDemo.swf" />
        <param name="menu" value="false" />
        <param name="wmode" value="transparent" />
        <param name="allowscriptaccess" value="always" />
        <!--[if !IE]>-->
        <object type="application/x-shockwave-flash" data="VRDemo.swf" width="1280" height="800" class="pano">
            <param name="menu" value="false" />
            <param name="wmode" value="transparent" />
            <param name="allowscriptaccess" value="always" />
            <param name="allownetworking" value="all" />
            <param name="flashvars" value="zoom=null&amp;pan=null&amp;sound=null" />
        <!--<![endif]-->
            <a href="http://www.adobe.com/go/getflashplayer">
                <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
            </a>
        <!--[if !IE]>-->
        </object>
        <!--<![endif]-->
    </object>
</div>

<script>

var flashObj; 

$(document).ready(function(){

    flashObj = document.getElementById('tourFlash');

    $('#interface').click(function(){
        console.log('click');
        talkToFlash();
    });
});

function talkToFlash(){
    flashObj.listenToJS('hello from js');
}

function listenFromFlash(flashMessage){
    console.log(message);
}
</script>

The click handler is triggered, but here I get the error. My flash file uses a document class, and within the document class is the public function. Flash is structured like this:

点击处理程序被触发,但在这里我得到了错误。我的 flash 文件使用了一个文档类,在文档类中是公共函数。Flash 的结构如下:

package com.company.vr {

    import flash.display.*;
    import flash.events.*; 
    import com.greensock.*;
    import com.greensock.easing.*;
    import flash.external.ExternalInterface;
    import flash.system.Security;

    Security.allowDomain("*");

     public class VR_TestDocument extends MovieClip {
            public function VR_TestDocument() {
              ExternalInterface.addCallback("talkToFlash", listenToJS);
            }


            public function listenToJS(message){
              trace ("from js: " + message);
              var flashMessage = message + " flash";
              ExternalInterface.call("listenFromFlash", flashMessage);
            }
     }
}

---UPDATE---

- -更新 - -

It looks like External Interface doesn't like SWFObject for some reason. If I switch to the method of embedding that Flash used in this example:

由于某种原因,看起来外部接口不喜欢 SWFObject。如果我切换到本示例中使用的嵌入 Flash 的方法:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html#addCallback()

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html#addCallback()

it works, but I feel like swfobject is the best way to embed flash. Anyone got any ideas?

它有效,但我觉得 swfobject 是嵌入 Flash 的最佳方式。有人有任何想法吗?

采纳答案by Serge Him

If you embeded flash in html as your code above, note, that second tag objectalso has to contain attribute id, corrected code is here:

如果您在 html 中嵌入了 flash 作为上面的代码,请注意,第二个标记对象也必须包含属性id,更正的代码在这里:

<div id="flash_content">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="1280" height="800" id="tourFlash" name="pano" class="pano">
    <param name="movie" value="VRDemo.swf" />
    <param name="menu" value="false" />
    <param name="wmode" value="transparent" />
    <param name="allowscriptaccess" value="always" />
    <!--[if !IE]>-->
    <object type="application/x-shockwave-flash" data="VRDemo.swf" width="1280" height="800" class="pano" id="tourFlash1">
        <param name="menu" value="false" />
        <param name="wmode" value="transparent" />
        <param name="allowscriptaccess" value="always" />
        <param name="allownetworking" value="all" />
        <param name="flashvars" value="zoom=null&amp;pan=null&amp;sound=null" />
    <!--<![endif]-->
        <a href="http://www.adobe.com/go/getflashplayer">
            <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
        </a>
    <!--[if !IE]>-->
    </object>
    <!--<![endif]-->
</object>

But of course, swfobject is the best way to embed flash. Correct html code looks like:

但当然,swfobject 是嵌入 Flash 的最佳方式。正确的 html 代码如下所示:

    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
        <title>js</title>       
        <script type="text/javascript" src="swfobject.js"></script>
        <script>
            function talkToFlash(){
                document.getElementById('flash_content').listenToJS('hello from js');
            }

            var flashvars =  {};
            var params = {
                allowscriptaccess: "always"         
            }           
            var attributes =  {};
            swfobject.embedSWF("VRDemo.swf", "flash_content", "550", "400", "10.0.0", false, flashvars, params, attributes);
    </script>   
    </head>
    <body>
        <div id="flash_content"></div>
    </body>
</html>

--Update--

- 更新 -

You have to select the correct flash element on the page. (Depends on the browser). As an example, here is code to get correct flashObj:

您必须在页面上选择正确的 flash 元素。(取决于浏览器)。例如,这里是获取正确 flashObj 的代码:

flashObj1 = document.getElementById('tourFlash');
flashObj2 = document.getElementById('tourFlash1');
flashObj = flashObj1.talkToFlash != undefined ? flashObj1 : flashObj2;      

回答by pipwerks

It could very well be a timing issue -- Flash Player takes a little bit of time to initialize ExternalInterface, and then a little bit more time to load your SWF. Your example code is running on DOMReady, which is NOT a guarantee that the SWF has loaded or that ExternalInterface has initialized within Flash Player.

这很可能是时间问题——Flash Player 需要一些时间来初始化 ExternalInterface,然后需要更多时间来加载您的 SWF。您的示例代码在 DOMReady 上运行,这不能保证 SWF 已加载或 ExternalInterface 已在 Flash Player 中初始化。

I recommend querying the SWF to see if it has finished loading. There's an example (using dynamic publishing) on LearnSWFObject.com: http://learnswfobject.com/advanced-topics/executing-javascript-when-the-swf-has-finished-loading/

我建议查询 SWF 以查看它是否已完成加载。LearnSWFObject.com 上有一个示例(使用动态发布):http://learnswfobject.com/advanced-topics/executing-javascript-when-the-swf-has-finished-loading/

UPDATE: After re-reading your code and some other suggested answers, I noticed you're using document.getElementByIdto grab your <object>. When using the nested <object>markup, this won't work (except in IE, which uses the outer <object>). swfobject.getObjectByIdwas specifically created to address this issue. Try editing your JS to use swfobject.getObjectById.

更新:重新阅读你的代码和其他一些参考答案后,我注意到你正在使用document.getElementById抓住你<object>。使用嵌套<object>标记时,这将不起作用(IE 除外,它使用外部<object>)。swfobject.getObjectById专为解决此问题而创建。尝试编辑您的 JS 以使用swfobject.getObjectById.