调用 flex-AS 函数时出现 Javascript 错误

发布于 2024-11-01 17:41:16 字数 2027 浏览 2 评论 0原文

我正在尝试从 JS 调用调用操作脚本函数,但收到以下错误

   Error: getFlashMovie(swfobjectID).sayWhat is not a function

任何人都可以让我知道我在这里做错了什么

     <html>

     <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="myMovieName" WIDTH="225" HEIGHT="200">
     <PARAM NAME="movie" VALUE="ax.swf" /> <PARAM NAME="quality" VALUE="high" /> <PARAM NAME="bgcolor" VALUE="#FFFFFF" /> <EMBED href="ax.swf" src="ax.swf" quality=high bgcolor=#FFFFFF NAME="myMovieName" ALIGN="" TYPE="application/x-shockwave-flash"> </EMBED> </OBJECT>



     <script>
     function getFlashMovie(movieName) {
        alert("In get Flash Movie");
         document.getElementById(movieName).setAttribute("name", movieName);
         var isIE = navigator.appName.indexOf("Microsoft") != -1;
         return (isIE) ? window[movieName] : document[movieName];
     }

     function sayWhat()
     {
        alert("In call as");
        var swfobjectID = 'myMovieName';  
        alert(swfobjectID);
        //call flex function 
        getFlashMovie(swfobjectID).sayWhat();
     }
     </script>
     <input type="button" onclick="javascript:sayWhat();" value="Click Me" />

     </html>

MXML

  <?xml version="1.0" encoding="utf-8"?>
  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initCamera()">

  <mx:Script>
  <![CDATA[

  import mx.controls.Button;
  import mx.controls.Alert;
  import flash.display.InteractiveObject;
  import flash.display.Sprite;
  import flash.media.*;
  import flash.net.*;
  import flash.external.*;
  import flash.external.ExternalInterface;



  public function sayWhat():void {
  Alert.show("Hi");
  }

  public function initCamera():void {
  //stop();
  ExternalInterface.addCallback("sayWhat", sayWhat);

  }        


   ]]>               
  </mx:Script>
  </mx:Application>

I am trying to call call action script function from JS but i get the following error

   Error: getFlashMovie(swfobjectID).sayWhat is not a function

Can any body let me know what am i doing wrong here

     <html>

     <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="myMovieName" WIDTH="225" HEIGHT="200">
     <PARAM NAME="movie" VALUE="ax.swf" /> <PARAM NAME="quality" VALUE="high" /> <PARAM NAME="bgcolor" VALUE="#FFFFFF" /> <EMBED href="ax.swf" src="ax.swf" quality=high bgcolor=#FFFFFF NAME="myMovieName" ALIGN="" TYPE="application/x-shockwave-flash"> </EMBED> </OBJECT>



     <script>
     function getFlashMovie(movieName) {
        alert("In get Flash Movie");
         document.getElementById(movieName).setAttribute("name", movieName);
         var isIE = navigator.appName.indexOf("Microsoft") != -1;
         return (isIE) ? window[movieName] : document[movieName];
     }

     function sayWhat()
     {
        alert("In call as");
        var swfobjectID = 'myMovieName';  
        alert(swfobjectID);
        //call flex function 
        getFlashMovie(swfobjectID).sayWhat();
     }
     </script>
     <input type="button" onclick="javascript:sayWhat();" value="Click Me" />

     </html>

MXML

  <?xml version="1.0" encoding="utf-8"?>
  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initCamera()">

  <mx:Script>
  <![CDATA[

  import mx.controls.Button;
  import mx.controls.Alert;
  import flash.display.InteractiveObject;
  import flash.display.Sprite;
  import flash.media.*;
  import flash.net.*;
  import flash.external.*;
  import flash.external.ExternalInterface;



  public function sayWhat():void {
  Alert.show("Hi");
  }

  public function initCamera():void {
  //stop();
  ExternalInterface.addCallback("sayWhat", sayWhat);

  }        


   ]]>               
  </mx:Script>
  </mx:Application>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慈悲佛祖 2024-11-08 17:41:16

这是一个工作示例应用程序,它从 javascript 调用 Flex sayWhat 函数。

它包含 4 个文件:ax.mxmlcall_flfunc.htmlax.jsax.css。< br>
您应该将最后三个文件和生成的 ax.swf 文件放在 Web 服务器上的同一文件夹中(或修改它们引用的路径),它将起作用。

ax.mxml:主 Flex 应用程序的结构

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
    width="300" height="300" backgroundColor="#bee3f6"
    creationComplete="onCreationComplete()">
    <mx:Script>
        <![CDATA[

            import mx.controls.Alert;

            public function sayWhat():void {
                Alert.show("Hi");
            }

            public function onCreationComplete():void {
                Security.allowDomain("localhost");

                // binds this.sayWhat to the external interface, so when from   
                // javascript is called the compiled swf object's sayWhat function,
                // it will be transferred to this.sayWhat. 
                ExternalInterface.addCallback("sayWhat", sayWhat);

                // The next line tells the external interface (the parent application:
                // browser window), that this application has finished loading, its 
                // ready to be used.
                // In js there has to be a global method with this name.
                ExternalInterface.call("onFlashAppInited");
            }        

        ]]>             
    </mx:Script>
</mx:Application>

call_flfunc.html:嵌入 ax.swf 的 html 内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>AX</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <link href="ax.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div id="app">
            <div id="app-header">
                <input type="button" value="Call Flex function"
                    onclick="onButtonClick()" />
            </div>
        </div>
        <!-- tmp is a not displayed div element used just to hold the swf 
            until it gets rendered inside the app div -->
        <div id="tmp">
            <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="ax"
                width="300" height="300"
                codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
                <param name="movie" value="ax.swf" />
                <param name="quality" value="high" />
                <param name="bgcolor" value="#bee3f6" />
                <param name="allowScriptAccess" value="always" />
                <embed src="ax.swf" quality="high" bgcolor="#bee3f6" width="300"
                    height="300" name="ax" align="middle" play="true" loop="false"
                    quality="high" allowScriptAccess="always"
                    type="application/x-shockwave-flash"
                    pluginspage="http://www.adobe.com/go/getflashplayer"> </embed>
            </object>
        </div>
        <script type="text/javascript" src="ax.js"></script>
    </body>
</html>

ax.js:包含所有必需的 javascript 类和函数

function Util(){
    this.ua = navigator.userAgent.toLowerCase();
    this.isOpera = this.ua.indexOf("opera") > -1;
    this.isSafari = (/webkit|khtml/).test(this.ua);
    this.isIE = !this.isOpera && this.ua.indexOf("msie") > -1;
    this.isIE7 = !this.isOpera && this.ua.indexOf("msie 7") > -1;
    this.isGecko = !this.isSafari && this.ua.indexOf("gecko") > -1;
    this.ieVersion = parseInt(this.ua.substring(this.ua.indexOf("msie") + 4));
    if (this.ieVersion == 0) {
        this.ieVersion = 100;
    }
}
Util.prototype = {};
var util = new Util();

/**
 * 
 * @param id
 * @param swf
 * @param width
 * @param height
 * @param scriptAccess
 * @param allowFullscreen
 * @return
 */
function FO(id, swf, width, height, scriptAccess, allowFullscreen){
    this.id = id;
    this.movie = swf;
    this.height = height ? height : 180;
    this.width = width ? width : 240;
    this.scriptAccess = scriptAccess ? scriptAccess : "always";
    this.allowFullscr = allowFullscreen ? "true" : "false";
    this.obj = document.createElement("embed");
    this.obj.src = this.movie;
    this.obj.width = this.width;
    this.obj.height = this.height;
    this.obj.name = this.id;
    this.obj.id = this.id;
    this.obj.align = "middle";
    this.obj.type = "application/x-shockwave-flash";
    this.obj.setAttribute("quality", "high");
    this.obj.setAttribute("bgColor", "#bee3f6");
    this.obj.setAttribute("play", "true");
    this.obj.setAttribute("loop", "false");
    this.obj.setAttribute("allowScriptAccess", this.scriptAccess);
    this.obj.setAttribute("allowFullscreen", this.allowFullscr);
    this.obj.setAttribute("pluginspage", "http://www.adobe.com/go/getflashplayer");
}
FO.prototype = {
    id :null,
    width :null,
    height :null,
    movie :null,
    scriptAccess :null,
    allowFullscr :null,
    obj :null,

    addParam : function(name, value){
        var p = document.createElement("param");
        p.name = name;
        p.value = value;
        this.obj.appendChild(p);
    }
};
var app = document.getElementById("app");
var appInited = false;

function onButtonClick(){
    if (appInited)
        flashApp.sayWhat();
    else
        alert("Flash app not inited!");
}

function init(){
    if (util.isIE)
        flashApp = document.getElementById("ax");
    else 
        flashApp = new FO("ax", "ax.swf", 300, 300).obj;
    app.appendChild(flashApp);
}

function onFlashAppInited() {
    // alert("Flash app inited!");
    appInited = true;
    // remove the temporary swf container: tmp
    document.body.removeChild(document.getElementById("tmp"));
}
window.onload = init;

ax.css:应用于 call_flfunc.html 页面的样式表

html, body {
    width: 100% !important;
    height: 100%;
    padding: 0px;
    margin: 0px;
    overflow: hidden;
    text-align: center;
}
body {
    overflow: auto;
    text-align: center;
}
object, embed {
    margin: 0px !important;
    padding: 0px !important;
}
#app {
    margin-left: auto;
    margin-right: auto;
    margin-top: 2%;
    width: 1000px;
    height: 545px;
    text-align: center;
}
#app-header {
    margin-left: auto;
    margin-right: auto;
    width: 1000px !important;
    text-align: center;
    height: 23px;
    line-height: 23px;
    overflow: hidden;
    white-space: nowrap;
    text-align: center;
}
#tmp {
    display: none;
    visible: false;
}

我希望它是可以理解的,您可以从中进行工作。

Here is a working sample application that calls your flex sayWhat funciton from javascript.

It contains 4 files: ax.mxml, call_flfunc.html, ax.js and ax.css.
You should put the last three files and the generated ax.swf file in the same folder on your web server (or modify their path where they are referenced), and it will work.

ax.mxml: your main flex application's structure

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
    width="300" height="300" backgroundColor="#bee3f6"
    creationComplete="onCreationComplete()">
    <mx:Script>
        <![CDATA[

            import mx.controls.Alert;

            public function sayWhat():void {
                Alert.show("Hi");
            }

            public function onCreationComplete():void {
                Security.allowDomain("localhost");

                // binds this.sayWhat to the external interface, so when from   
                // javascript is called the compiled swf object's sayWhat function,
                // it will be transferred to this.sayWhat. 
                ExternalInterface.addCallback("sayWhat", sayWhat);

                // The next line tells the external interface (the parent application:
                // browser window), that this application has finished loading, its 
                // ready to be used.
                // In js there has to be a global method with this name.
                ExternalInterface.call("onFlashAppInited");
            }        

        ]]>             
    </mx:Script>
</mx:Application>

call_flfunc.html: the html content into which the ax.swf is embedded

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>AX</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <link href="ax.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div id="app">
            <div id="app-header">
                <input type="button" value="Call Flex function"
                    onclick="onButtonClick()" />
            </div>
        </div>
        <!-- tmp is a not displayed div element used just to hold the swf 
            until it gets rendered inside the app div -->
        <div id="tmp">
            <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="ax"
                width="300" height="300"
                codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
                <param name="movie" value="ax.swf" />
                <param name="quality" value="high" />
                <param name="bgcolor" value="#bee3f6" />
                <param name="allowScriptAccess" value="always" />
                <embed src="ax.swf" quality="high" bgcolor="#bee3f6" width="300"
                    height="300" name="ax" align="middle" play="true" loop="false"
                    quality="high" allowScriptAccess="always"
                    type="application/x-shockwave-flash"
                    pluginspage="http://www.adobe.com/go/getflashplayer"> </embed>
            </object>
        </div>
        <script type="text/javascript" src="ax.js"></script>
    </body>
</html>

ax.js: contains all the necessary javascript classes and functions

function Util(){
    this.ua = navigator.userAgent.toLowerCase();
    this.isOpera = this.ua.indexOf("opera") > -1;
    this.isSafari = (/webkit|khtml/).test(this.ua);
    this.isIE = !this.isOpera && this.ua.indexOf("msie") > -1;
    this.isIE7 = !this.isOpera && this.ua.indexOf("msie 7") > -1;
    this.isGecko = !this.isSafari && this.ua.indexOf("gecko") > -1;
    this.ieVersion = parseInt(this.ua.substring(this.ua.indexOf("msie") + 4));
    if (this.ieVersion == 0) {
        this.ieVersion = 100;
    }
}
Util.prototype = {};
var util = new Util();

/**
 * 
 * @param id
 * @param swf
 * @param width
 * @param height
 * @param scriptAccess
 * @param allowFullscreen
 * @return
 */
function FO(id, swf, width, height, scriptAccess, allowFullscreen){
    this.id = id;
    this.movie = swf;
    this.height = height ? height : 180;
    this.width = width ? width : 240;
    this.scriptAccess = scriptAccess ? scriptAccess : "always";
    this.allowFullscr = allowFullscreen ? "true" : "false";
    this.obj = document.createElement("embed");
    this.obj.src = this.movie;
    this.obj.width = this.width;
    this.obj.height = this.height;
    this.obj.name = this.id;
    this.obj.id = this.id;
    this.obj.align = "middle";
    this.obj.type = "application/x-shockwave-flash";
    this.obj.setAttribute("quality", "high");
    this.obj.setAttribute("bgColor", "#bee3f6");
    this.obj.setAttribute("play", "true");
    this.obj.setAttribute("loop", "false");
    this.obj.setAttribute("allowScriptAccess", this.scriptAccess);
    this.obj.setAttribute("allowFullscreen", this.allowFullscr);
    this.obj.setAttribute("pluginspage", "http://www.adobe.com/go/getflashplayer");
}
FO.prototype = {
    id :null,
    width :null,
    height :null,
    movie :null,
    scriptAccess :null,
    allowFullscr :null,
    obj :null,

    addParam : function(name, value){
        var p = document.createElement("param");
        p.name = name;
        p.value = value;
        this.obj.appendChild(p);
    }
};
var app = document.getElementById("app");
var appInited = false;

function onButtonClick(){
    if (appInited)
        flashApp.sayWhat();
    else
        alert("Flash app not inited!");
}

function init(){
    if (util.isIE)
        flashApp = document.getElementById("ax");
    else 
        flashApp = new FO("ax", "ax.swf", 300, 300).obj;
    app.appendChild(flashApp);
}

function onFlashAppInited() {
    // alert("Flash app inited!");
    appInited = true;
    // remove the temporary swf container: tmp
    document.body.removeChild(document.getElementById("tmp"));
}
window.onload = init;

ax.css: the style sheet applied to the call_flfunc.html page

html, body {
    width: 100% !important;
    height: 100%;
    padding: 0px;
    margin: 0px;
    overflow: hidden;
    text-align: center;
}
body {
    overflow: auto;
    text-align: center;
}
object, embed {
    margin: 0px !important;
    padding: 0px !important;
}
#app {
    margin-left: auto;
    margin-right: auto;
    margin-top: 2%;
    width: 1000px;
    height: 545px;
    text-align: center;
}
#app-header {
    margin-left: auto;
    margin-right: auto;
    width: 1000px !important;
    text-align: center;
    height: 23px;
    line-height: 23px;
    overflow: hidden;
    white-space: nowrap;
    text-align: center;
}
#tmp {
    display: none;
    visible: false;
}

I hope it's understandable, and you can work from it.

溺渁∝ 2024-11-08 17:41:16

您可能需要在 js 中使用其他类型的解决方法来从任何浏览器调用 flash 函数。

在这里我分享我的实现方法(希望它还没有被弃用......)。你需要一个 javascript 类(我将其命名为 FO):

/**
 * 
 * @param id
 * @param swf
 * @param width
 * @param height
 * @param scriptAccess
 * @param allowFullscreen
 * @return
 */
function FO(id, swf, width, height, scriptAccess, allowFullscreen){
    this.id = id;
    this.movie = swf;
    this.height = height ? height : 180;
    this.width = width ? width : 240;
    this.scriptAccess = scriptAccess ? scriptAccess : "always";
    this.allowFullscr = allowFullscreen ? "true" : "false";
    this.obj = document.createElement("embed");
    this.obj.src = this.movie;
    this.obj.width = this.width;
    this.obj.height = this.height;
    this.obj.name = this.id;
    this.obj.id = this.id;
    this.obj.align = "middle";
    this.obj.type = "application/x-shockwave-flash";
    this.obj.setAttribute("quality", "high");
    this.obj.setAttribute("bgColor", "#bee3f6");
    this.obj.setAttribute("play", "true");
    this.obj.setAttribute("loop", "false");
    this.obj.setAttribute("allowScriptAccess", this.scriptAccess);
    this.obj.setAttribute("allowFullscreen", this.allowFullscr);
    this.obj.setAttribute("pluginspage", "http://www.adobe.com/go/getflashplayer");
}
FO.prototype = {
    id :null,
    width :null,
    height :null,
    movie :null,
    scriptAccess :null,
    allowFullscr :null,
    obj :null,

    addParam : function(name, value){
        var p = document.createElement("param");
        p.name = name;
        p.value = value;
        this.obj.appendChild(p);
    }
};

你将需要一个初始化方法(或扩展现有的方法):

// should be called on window.onload
function init() {
    var flashObject = null;
    if (isIE)
        flashObject = document.getElementById("myMovieName");
    else 
    {
        flashObject = new FO("myMovieName", "ax.swf", 225, 200).obj;
        // **app** is the id of a div / html container element, 
        // in which your <object> is positioned. 
        document.getElementById("app").appendChild(flashObject);
    }
}
window.onload = init;

以及调用所需的方法:

//somewhere else in your code:
if (flashObject)
    flashObject.sayWhat();

You might need an other kind of workaround in js to call the flash function from any browser.

Here i share my way to accomplish this (hope it is not deprecated yet...). You need a javascript class (i named it FO):

/**
 * 
 * @param id
 * @param swf
 * @param width
 * @param height
 * @param scriptAccess
 * @param allowFullscreen
 * @return
 */
function FO(id, swf, width, height, scriptAccess, allowFullscreen){
    this.id = id;
    this.movie = swf;
    this.height = height ? height : 180;
    this.width = width ? width : 240;
    this.scriptAccess = scriptAccess ? scriptAccess : "always";
    this.allowFullscr = allowFullscreen ? "true" : "false";
    this.obj = document.createElement("embed");
    this.obj.src = this.movie;
    this.obj.width = this.width;
    this.obj.height = this.height;
    this.obj.name = this.id;
    this.obj.id = this.id;
    this.obj.align = "middle";
    this.obj.type = "application/x-shockwave-flash";
    this.obj.setAttribute("quality", "high");
    this.obj.setAttribute("bgColor", "#bee3f6");
    this.obj.setAttribute("play", "true");
    this.obj.setAttribute("loop", "false");
    this.obj.setAttribute("allowScriptAccess", this.scriptAccess);
    this.obj.setAttribute("allowFullscreen", this.allowFullscr);
    this.obj.setAttribute("pluginspage", "http://www.adobe.com/go/getflashplayer");
}
FO.prototype = {
    id :null,
    width :null,
    height :null,
    movie :null,
    scriptAccess :null,
    allowFullscr :null,
    obj :null,

    addParam : function(name, value){
        var p = document.createElement("param");
        p.name = name;
        p.value = value;
        this.obj.appendChild(p);
    }
};

You will need an initialization method (or extend the existing one):

// should be called on window.onload
function init() {
    var flashObject = null;
    if (isIE)
        flashObject = document.getElementById("myMovieName");
    else 
    {
        flashObject = new FO("myMovieName", "ax.swf", 225, 200).obj;
        // **app** is the id of a div / html container element, 
        // in which your <object> is positioned. 
        document.getElementById("app").appendChild(flashObject);
    }
}
window.onload = init;

and for calling the desired method:

//somewhere else in your code:
if (flashObject)
    flashObject.sayWhat();
森林迷了鹿 2024-11-08 17:41:16

从 javascript 调用时尝试:

myMovieName.sayWhat();

When calling from javascript try:

myMovieName.sayWhat();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文