从JavaScript AS3函数调用函数、JavaScript

2023-09-08 15:04:27 作者:水煮美人鱼

我想从JavaScript调用一个函数AS3,但得到以下错误在浏览器:

I am trying to call a AS3 function from javascript but getting the following error in browser:

对象亘古不支持属性或方法myCreateFile。

Object doesnot support property or method myCreateFile .

下面是AS3类:

package {
import flash.display.Sprite;
    import flash.external.ExternalInterface;
    import flash.net.FileReference;             
import flash.events.IOErrorEvent;
import flash.events.Event;
import flash.system.Security;

public class CreateDoc extends Sprite {
    private static const DEFAULT_FILE_NAME:String = "example.txt";

    //FileReference Class well will use to save data
    private var fr:FileReference;

    public function CreateDoc()
    {
        // Register the function for external use.
        ExternalInterface.addCallback("myCreateFile", myCreateFile);
        Security.allowDomain("*");
    }

    public function myCreateFile():void
    {
        fr = new FileReference();
        //open a native save file dialog, using the default file name
        fr.save("Demo file", DEFAULT_FILE_NAME);

        fr = null;         
    }
}
}

HTML code:

HTML code:

<html>
<head>      
    <script type="text/javascript" src="swfobject.js"></script>
    <script type="text/javascript">
        try{
            var flashvars = {};
            var params = {allowscriptaccess:"always", movie:"CreateDoc.swf", wmode:"opaque", menu:"false"};
            var attributes = {id:"flashcontent",  name:"flashcontent"};
            swfobject.embedSWF("CreateDoc.swf", "flashcontent", "800", "600", "10.0.0", "expressInstall.swf", flashvars, params, attributes);
        }
        catch(err){
            alert(err.message);
        }
    </script>

    <script type="text/javascript">
        function doFunction(){
            alert('Calling function..');
            try{
                var myObj = document.getElementById("flashcontent");
                myObj.myCreateFile();
            }
            catch(err){
                alert(err.message);
            }
        }
    </script>
</head>
<body>
    <div id="flashcontent">
    </div>

    <input id="save file" type="button" value="clickme" onclick="doFunction();" />
</body>

任何想法是错误的,当我尝试调用myCreateFile()AS3函数,它是从Java脚本CreateDoc类present?

Any idea what is wrong when I try to call the myCreateFile() AS3 function which is present in CreateDoc class from java script?

推荐答案

现在的问题是,你已经在三个地方使用相同的 ID 。更改 flashcontent 在这里: swfobject.embedSWF(CreateDoc.swf,flashcontent,... 别的东西, UNIQUE_ID 例如,所以这将是: swfobject.embedSWF(CreateDoc.swf,UNIQUE_ID...... 使用后这个ID在这里:文档.getElementById(flashcontent); 太像的document.getElementById(UNIQUE_ID);

The problem is that you have used same id in three places. Change "flashcontent" here: swfobject.embedSWF("CreateDoc.swf", "flashcontent" , ... to something else , unique_id for example , so it will be: swfobject.embedSWF("CreateDoc.swf", "unique_id" ... . After that use this id here : document.getElementById("flashcontent"); too , like document.getElementById("unique_id");