Firefox的Flash外部接口问题

时间:2022-03-27 22:42:07

I am having a hard time getting ExternalInterface to work on Firefox. I am trying to call a AS3 function from javascript. The SWF is setup with the right callbacks and it is working in IE.

我很难将ExternalInterface用于Firefox。我试图从javascript调用AS3函数。 SWF设置正确的回调,它在IE中工作。

I am using AC_RunActiveContent.js to embed the swf into my page. However, I have modified it to add an ID to the Object / Embed Tags. Below are object and embed tag that are generated for IE and for Firefox respectively.

我正在使用AC_RunActiveContent.js将swf嵌入到我的页面中。但是,我已修改它以向Object / Embed Tags添加ID。下面是分别为IE和Firefox生成的object和embed标记。

    <object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="400" height="400" align="middle" id="jpeg_encoder2" name="jpeg_encoder3" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" >
    <param name="movie" value="/jpeg_encoder/jpeg_encoder3.swf" /> 
    <param name="quality" value="high" /> 
    <param name="play" value="true" /> 
    <param name="loop" value="true" /> 
    <param name="scale" value="showall" /> 
    <param name="wmode" value="window" /> 
    <param name="devicefont" value="false" /> 
    <param name="bgcolor" value="#ffffff" /> 
    <param name="menu" value="false" /> 
    <param name="allowFullScreen" value="false" /> 
    <param name="allowScriptAccess" value="always" /> 
</object>


<embed 
    width="400" 
    height="400" 
    src="/jpeg_encoder/jpeg_encoder3.swf" 
    quality="high" 
    pluginspage="http://www.macromedia.com/go/getflashplayer" 
    align="middle" 
    play="true" 
    loop="true" 
    scale="showall" 
    wmode="window" 
    devicefont="false" 
    id="jpeg_encoder2" 
    bgcolor="#ffffff" 
    name="jpeg_encoder3" 
    menu="false" 
    allowFullScreen="false" 
    allowScriptAccess="always" 
    type="application/x-shockwave-flash" > 
</embed>

I am calling the function like this...

我这样称呼函数......

<script>
try {
    document.getElementById('jpeg_encoder2').processImage(z);
} catch (e) { alert(e.message); }
</script>

In Firefox, I get an error saying "document.getElementById("jpeg_encoder2").processImage is not a function"

在Firefox中,我收到一条错误消息“document.getElementById(”jpeg_encoder2“)。processImage不是函数”

Any Ideas?

7 个解决方案

#1


Hmm, did you expose your actionscript function to Javascript with addCallback ?

嗯,您是否使用addCallback将您的actionscript函数暴露给Javascript?

Adobe documentation on addCallback

关于addCallback的Adobe文档

#2


Below is an example of how a Flash movie is placed within a html page. This movie is very simple movie with a stop action at the beginning. The movie is shown below under Test Runs subject. This particular html code was auto generated by FlashMX's Publish command. Notice that the Flash movie file is simplemovie.swf; and an id and a name have been assigned automatically by Flash to match the movie filename (minus the .swf extension). In reality, the name and id could be anything (but do not use exoteric names, especially, do not start with a number), as long as it has not been used by any other element in the same page.

下面是Flash影片放置在html页面中的示例。这部电影是非常简单的电影,一开始就有停止动作。电影如下所示在测试运行主题下。这个特殊的html代码是由FlashMX的Publish命令自动生成的。请注意,Flash影片文件是simplemovie.swf;并且Flash已自动分配ID和名称以匹配电影文件名(减去.swf扩展名)。实际上,名称和id可以是任何东西(但不要使用开放的名称,尤其是,不要以数字开头),只要它没有被同一页面中的任何其他元素使用。

`codebase="http://download.macromedia.com/pub/shockwave/cabs/flash`/swflash.cab#version=6,0,0,0" ` WIDTH="150" HEIGHT="75" id="simplemovie" ALIGN=""> ` quality=medium ` swliveconnect="true" ` bgcolor=#FFFFFF WIDTH="150" HEIGHT="75" ` name="simplemovie" ` ALIGN="" `TYPE="application/x-shockwave-flash" ` PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">

`codebase =“http://download.macromedia.com/pub/shockwave/cabs/flash`/swflash.cab#version=6,0,0,0”`WIDTH =“150”HEIGHT =“75”id = “simplemovie”ALIGN =“”>`quality = medium`swliveconnect =“true”`bgcolor =#FFFFFF WIDTH =“150”HEIGHT =“75”`name =“simplemovie”`ALIGN =“”`TYPE =“application / x-shockwave-flash“`PLUGINSPAGE =”http://www.macromedia.com/go/getflashplayer“>

there is a play function in the flash file the following function will calls that function:

flash文件中有一个播放功能,以下函数将调用该函数:

function testEval(stringToEval)
{
  var movie=eval(stringToEval);
  if (movie)
  {
    if (movie.PercentLoaded())
    {
      if (movie.PercentLoaded()==100)
      {
        movie.Play();
      }
      else
      {
        alert("movie is still loading, try again soon");
      }
    }
    else
    {
      alert("movie object found-but unable to send command"); 
    }
  }
  else
  {
    alert("movie object not found");
  }
}

#3


See this post.

看这篇文章。

You want a JS function more like this to retrieve the Flash object (rather than getElemById):

你想要一个更像这样的JS函数来检索Flash对象(而不是getElemById):

function thisMovie(movieName) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName];
  } else {
    return document[movieName];
  }
};

And make sure not to call this function until the document is loaded.

并确保在加载文档之前不要调用此函数。

#4


Try having the same id in both object and embed tags. I remember one browser is using one tag and another browser the other...don't know which one is which. I had the same issue some time ago.

尝试在object和embed标记中使用相同的id。我记得有一个浏览器使用一个标签而另一个浏览器使用另一个浏览器...不知道哪个是哪个。我前段时间遇到过同样的问题。

I'm got around by modifying the Example Code that comes with flash. Making sure it works, then stripping it down and adapting it for my use.

我通过修改flash附带的示例代码来解决这个问题。确保它有效,然后将其剥离并根据我的需要进行调整。

In the example notice that the object tag the id set to "ExternalInterfaceExample", then the embed tag has the name parameter set to "ExternalInterfaceExample" as well. I think that might be your clue.

在示例中,通知对象标记id设置为“ExternalInterfaceExample”,然后embed标记的name参数也设置为“ExternalInterfaceExample”。我想这可能是你的线索。

Good luck!

#5


Are your swf's visible (on the page) before you try calling them? If not, read this: swf-not-initializing-until-visible

在您尝试呼叫之前,您的SWF是否可见(在页面上)?如果没有,请阅读:swf-not-initializing-until-visible

#6


Try these two things:

试试这两件事:

First, try calling the function from Javascript like this:

首先,尝试从Javascript调用函数,如下所示:

var swf;
if(navigator.appName.indexOf("Microsoft") != -1) {
    swf = window["jpeg_encoder2"];
} else {
    swf = document["jpeg_encoder2"];
}
if(typeof(swf) == "undefined") swf = document.getElementById("jpeg_encoder2");

swf.processImage(z);

Second, I've found that ExternalInterface calls in Firefox seem to only work with embed tags, not object tags. See if you can get it to work if you just use an embed tag. (Right now, I'm not clear whether the HTML/Javascript combo you've posted will access the object or the embed element.)

其次,我发现Firefox中的ExternalInterface调用似乎只适用于嵌入标记,而不是对象标记。如果您只使用嵌入标记,请查看是否可以使其工作。 (现在,我不清楚你发布的HTML / Javascript组合是否会访问对象或embed元素。)

#7


If instead of using AC_RunActiveContent.js to embed your flash movie you use swfobject there is a easy build-in way of doing that.

如果不是使用AC_RunActiveContent.js嵌入你的flash影片,你使用swfobject有一个简单的内置方式来做到这一点。

<head>
 <script type="text/javascript" src="swfobject.js"></script>

 <script type="text/javascript">
 try {
      swfobject.getObjectById('jpeg_encoder2').processImage(z); 
      } catch (e) { alert(e.message); }
 </script>
</head>

If you are not using swfObject you could just copy and paste swfobject.getObjectById straight in to your code:

如果你没有使用swfObject,你可以直接将swfobject.getObjectById复制并粘贴到你的代码中:

<script type="text/javascript">

function getObjectById(objectIdStr) {
        var r = null;
        var o = getElementById(objectIdStr);
        if (o && o.nodeName == "OBJECT") {
            if (typeof o.SetVariable != UNDEF) {
                r = o;
            }
            else {
                var n = o.getElementsByTagName(OBJECT)[0];
                if (n) {
                    r = n;
                }
            }
        }
        return r;

getObjectById('jpeg_encoder2').processImage(z); //call your method 
</script>

#1


Hmm, did you expose your actionscript function to Javascript with addCallback ?

嗯,您是否使用addCallback将您的actionscript函数暴露给Javascript?

Adobe documentation on addCallback

关于addCallback的Adobe文档

#2


Below is an example of how a Flash movie is placed within a html page. This movie is very simple movie with a stop action at the beginning. The movie is shown below under Test Runs subject. This particular html code was auto generated by FlashMX's Publish command. Notice that the Flash movie file is simplemovie.swf; and an id and a name have been assigned automatically by Flash to match the movie filename (minus the .swf extension). In reality, the name and id could be anything (but do not use exoteric names, especially, do not start with a number), as long as it has not been used by any other element in the same page.

下面是Flash影片放置在html页面中的示例。这部电影是非常简单的电影,一开始就有停止动作。电影如下所示在测试运行主题下。这个特殊的html代码是由FlashMX的Publish命令自动生成的。请注意,Flash影片文件是simplemovie.swf;并且Flash已自动分配ID和名称以匹配电影文件名(减去.swf扩展名)。实际上,名称和id可以是任何东西(但不要使用开放的名称,尤其是,不要以数字开头),只要它没有被同一页面中的任何其他元素使用。

`codebase="http://download.macromedia.com/pub/shockwave/cabs/flash`/swflash.cab#version=6,0,0,0" ` WIDTH="150" HEIGHT="75" id="simplemovie" ALIGN=""> ` quality=medium ` swliveconnect="true" ` bgcolor=#FFFFFF WIDTH="150" HEIGHT="75" ` name="simplemovie" ` ALIGN="" `TYPE="application/x-shockwave-flash" ` PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">

`codebase =“http://download.macromedia.com/pub/shockwave/cabs/flash`/swflash.cab#version=6,0,0,0”`WIDTH =“150”HEIGHT =“75”id = “simplemovie”ALIGN =“”>`quality = medium`swliveconnect =“true”`bgcolor =#FFFFFF WIDTH =“150”HEIGHT =“75”`name =“simplemovie”`ALIGN =“”`TYPE =“application / x-shockwave-flash“`PLUGINSPAGE =”http://www.macromedia.com/go/getflashplayer“>

there is a play function in the flash file the following function will calls that function:

flash文件中有一个播放功能,以下函数将调用该函数:

function testEval(stringToEval)
{
  var movie=eval(stringToEval);
  if (movie)
  {
    if (movie.PercentLoaded())
    {
      if (movie.PercentLoaded()==100)
      {
        movie.Play();
      }
      else
      {
        alert("movie is still loading, try again soon");
      }
    }
    else
    {
      alert("movie object found-but unable to send command"); 
    }
  }
  else
  {
    alert("movie object not found");
  }
}

#3


See this post.

看这篇文章。

You want a JS function more like this to retrieve the Flash object (rather than getElemById):

你想要一个更像这样的JS函数来检索Flash对象(而不是getElemById):

function thisMovie(movieName) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[movieName];
  } else {
    return document[movieName];
  }
};

And make sure not to call this function until the document is loaded.

并确保在加载文档之前不要调用此函数。

#4


Try having the same id in both object and embed tags. I remember one browser is using one tag and another browser the other...don't know which one is which. I had the same issue some time ago.

尝试在object和embed标记中使用相同的id。我记得有一个浏览器使用一个标签而另一个浏览器使用另一个浏览器...不知道哪个是哪个。我前段时间遇到过同样的问题。

I'm got around by modifying the Example Code that comes with flash. Making sure it works, then stripping it down and adapting it for my use.

我通过修改flash附带的示例代码来解决这个问题。确保它有效,然后将其剥离并根据我的需要进行调整。

In the example notice that the object tag the id set to "ExternalInterfaceExample", then the embed tag has the name parameter set to "ExternalInterfaceExample" as well. I think that might be your clue.

在示例中,通知对象标记id设置为“ExternalInterfaceExample”,然后embed标记的name参数也设置为“ExternalInterfaceExample”。我想这可能是你的线索。

Good luck!

#5


Are your swf's visible (on the page) before you try calling them? If not, read this: swf-not-initializing-until-visible

在您尝试呼叫之前,您的SWF是否可见(在页面上)?如果没有,请阅读:swf-not-initializing-until-visible

#6


Try these two things:

试试这两件事:

First, try calling the function from Javascript like this:

首先,尝试从Javascript调用函数,如下所示:

var swf;
if(navigator.appName.indexOf("Microsoft") != -1) {
    swf = window["jpeg_encoder2"];
} else {
    swf = document["jpeg_encoder2"];
}
if(typeof(swf) == "undefined") swf = document.getElementById("jpeg_encoder2");

swf.processImage(z);

Second, I've found that ExternalInterface calls in Firefox seem to only work with embed tags, not object tags. See if you can get it to work if you just use an embed tag. (Right now, I'm not clear whether the HTML/Javascript combo you've posted will access the object or the embed element.)

其次,我发现Firefox中的ExternalInterface调用似乎只适用于嵌入标记,而不是对象标记。如果您只使用嵌入标记,请查看是否可以使其工作。 (现在,我不清楚你发布的HTML / Javascript组合是否会访问对象或embed元素。)

#7


If instead of using AC_RunActiveContent.js to embed your flash movie you use swfobject there is a easy build-in way of doing that.

如果不是使用AC_RunActiveContent.js嵌入你的flash影片,你使用swfobject有一个简单的内置方式来做到这一点。

<head>
 <script type="text/javascript" src="swfobject.js"></script>

 <script type="text/javascript">
 try {
      swfobject.getObjectById('jpeg_encoder2').processImage(z); 
      } catch (e) { alert(e.message); }
 </script>
</head>

If you are not using swfObject you could just copy and paste swfobject.getObjectById straight in to your code:

如果你没有使用swfObject,你可以直接将swfobject.getObjectById复制并粘贴到你的代码中:

<script type="text/javascript">

function getObjectById(objectIdStr) {
        var r = null;
        var o = getElementById(objectIdStr);
        if (o && o.nodeName == "OBJECT") {
            if (typeof o.SetVariable != UNDEF) {
                r = o;
            }
            else {
                var n = o.getElementsByTagName(OBJECT)[0];
                if (n) {
                    r = n;
                }
            }
        }
        return r;

getObjectById('jpeg_encoder2').processImage(z); //call your method 
</script>