如何让JavaScript来使用闪光灯(对象#&LT通信; HTMLObjectElement>没有方法闪光灯、对象、通信、方法

2023-09-08 14:29:00 作者:桃气泡泡

我试图从JavaScript发送一个简单的测试消息,闪存,但我得到的错误:

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

Object #<HTMLObjectElement> has no method "listenToJS"

我读过一些关于栈这个问题,但我觉得无论是浏览器没有得到适当的引用我的Flash对象,或我的动作中,我没有把我的闪光灯功能,在适当的位置。

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.

所以,在HTML中,我嵌入闪光灯SWFObj:

So within html I am embedding flash with SWFObj:

<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="https://m.xsw88.com/allimgs/daicuo/20230908/993.png.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>

单击处理程序被触发,但在这里我得到的错误。我的Flash文件使用一个文档类,文档类中的公共职能。闪光的结构是这样的:

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:

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---

它看起来像外部接口不喜欢SWFObject的出于某种原因。如果切换到嵌入该闪存本示例中使用的方法:

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:

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

它的工作原理,但我觉得SWFObject的是嵌入闪存的最佳途径。任何人有什么想法?

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

推荐答案

如果您嵌入式闪存的HTML作为code以上,请注意,这第二个标记对象也必须包含的属性< STRONG> ID ,纠正code是在这里:

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

<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="https://m.xsw88.com/allimgs/daicuo/20230908/993.png.gif" alt="Get Adobe Flash player" />
        </a>
    <!--[if !IE]>-->
    </object>
    <!--<![endif]-->
</object>

但当然,SWFObject的是嵌入闪存的最佳方式。正确的HTML code是这样的:

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

    <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--

您必须选择页面上的正确的闪光元素。 (取决于浏览器)。作为一个例子,这里是code,以获得正确的flashObj:

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:

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