0

Please pardon the dumb newbie question; I've only been programming Javascript for about 24 hours... :)

I have managed (with some outside help) to embed a shockwave flash object into my page - specifically MultiPowUpload 2.1. It's appearing nicely on the page and seems to be functioning correctly; we can take that part as read.

Now I'm trying to do some more advanced stuff in JavaScript - I have a button, and in the onClick() code I have the following:

var swf = document.getElementById("FlashFilesUpload1");
var itemsCount = swf.filesCount;
alert("items: "+itemsCount);

But the alert statement tells me "items: undefined". It does the same if I set itemsCount to swf.nonExistentProperty - everything is "undefined". But from what I read in the documentation, there definitely is a filesCount property. I tried accessing all the other properties and methods of this swf object, and none of them work, except for toString(), which returns "HTMLObjectElement".

I am obviously addressing this object incorrectly - but what am I doing wrong?


Clarification

In response to @brianpeiris: I am doing this quite differently from the page at the link you suggested. I didn't do this with any great knowledge, I'm just really acting like a monkey, trying to copy other people's code that works, then tweak it so it does what I want... so don't assume I know anything, however obvious - remember, I've only been doing JavaScript for about a day now!

So here's the code for the control itself:

          <object id="FlashFilesUpload1" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"
          classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" viewastext>
          <!-- Replace symbols " with the &quot; at all parameters values and 
    symbols "&" with the "%26" at URL values or &amp; at other values!
    The same parameters values should be set for EMBED object below. -->
          <param name="FlashVars" value="uploadButtonVisible=false&uploadUrl=../ReceiveBulkCases.aspx" />
          <param name="BGColor" value="#F8F6E6" />
          <param name="Movie" value="ClientSideControls/ElementITMultiPowUpload2.1.swf" />
          <param name="Src" value="ClientSideControls/ElementITMultiPowUpload2.1.swf" />
          <param name="WMode" value="Window" />
          <param name="Play" value="-1" />
          <param name="Loop" value="-1" />
          <param name="Quality" value="High" />
          <param name="SAlign" value="" />
          <param name="Menu" value="-1" />
          <param name="Base" value="" />
          <param name="AllowScriptAccess" value="always" />
          <param name="Scale" value="ShowAll" />
          <param name="DeviceFont" value="0" />
          <param name="EmbedMovie" value="0" />
          <param name="SWRemote" value="" />
          <param name="MovieData" value="" />
          <param name="SeamlessTabbing" value="1" />
          <param name="Profile" value="0" />
          <param name="ProfileAddress" value="" />
          <param name="ProfilePort" value="0" />
          <!-- Embed for Netscape,Mozilla/FireFox browsers support. Flashvars parameters are the same.-->
          <!-- Replace symbols " with the &quot; at all parameters values and symbols "&" with the "%26" at URL values or &amp; at other values! -->
          <embed bgcolor="#F8F6E6" id="EmbedFlashFilesUpload" src="ClientSideControls/ElementITMultiPowUpload2.1.swf"
            quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"
            type="application/x-shockwave-flash" width="450" height="350" flashvars="uploadButtonVisible=false&uploadUrl=../ReceiveBulkCases.aspx">
</embed>
        </object>

As you see it's embedded in the HTML as an object, unlike the reference you gave, where they do some other trick using script to get it onto the page.

BTW, when I tried it their way (not very hard, I grant you), I didn't manage to get the control even to appear on my page!

Thanks for your help... much appreciated

4

1 回答 1

2

编辑:我想我终于想通了!您只需将 设置useExternalInterfacesYes

所以在你的代码中你会改变

<param 
  name="FlashVars"
  value="uploadButtonVisible=false&uploadUrl=../ReceiveBulkCases.aspx" />

<param 
  name="FlashVars"
  value="uploadButtonVisible=false&uploadUrl=../ReceiveBulkCases.aspx&useExternalInterface=Yes" />

并对<embed>标签做同样的事情。

如果您访问演示站点并在 Firebug 中运行以下代码,它会0在您选择文件之前返回,并在您选择文件后返回正确的计数。此外,如果您检查来源,您会看到该useExternalInterface选项设置为Yes

alert(document.getElementById('MultiPowUpload').filesCount());

PS你应该考虑使用 MultiPowUpload 中包含的 SWFObject 脚本。它使您可以轻松设置和更改变量,而不必担心跨浏览器问题,并且它还可以为没有闪存的用户优雅地降级。


进一步编辑 回答您的评论:是的,我已经filesCount在我的机器上工作了。

我忘记提到的一件事是,您可能正在尝试filesCount在 Flash 控件完全加载之前进行检索。

以下是我正在使用的代码。我完全复制了您的代码并添加了useExternalInterface设置以及我自己的 javascript。

请注意,我的 javascript 代码使用该函数filesCount每 500 毫秒重复检查一次。setInterval

<object id="FlashFilesUpload1" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"
  classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" viewastext>
  <!-- Replace symbols " with the &quot; at all parameters values and
  symbols "&" with the "%26" at URL values or &amp; at other values!
  The same parameters values should be set for EMBED object below. -->
  <param name="FlashVars" value="uploadButtonVisible=false&uploadUrl=../ReceiveBulkCases.aspx&useExternalInterface=Yes" />
  <param name="BGColor" value="#F8F6E6" />
  <param name="Movie" value="ClientSideControls/ElementITMultiPowUpload2.1.swf" />
  <param name="Src" value="ClientSideControls/ElementITMultiPowUpload2.1.swf" />
  <param name="WMode" value="Window" />
  <param name="Play" value="-1" />
  <param name="Loop" value="-1" />
  <param name="Quality" value="High" />
  <param name="SAlign" value="" />
  <param name="Menu" value="-1" />
  <param name="Base" value="" />
  <param name="AllowScriptAccess" value="always" />
  <param name="Scale" value="ShowAll" />
  <param name="DeviceFont" value="0" />
  <param name="EmbedMovie" value="0" />
  <param name="SWRemote" value="" />
  <param name="MovieData" value="" />
  <param name="SeamlessTabbing" value="1" />
  <param name="Profile" value="0" />
  <param name="ProfileAddress" value="" />
  <param name="ProfilePort" value="0" />
  <!-- Embed for Netscape,Mozilla/FireFox browsers support. Flashvars parameters are the same.-->
  <!-- Replace symbols " with the &quot; at all parameters values and symbols "&" with the "%26" at URL values or &amp; at other values! -->
  <embed bgcolor="#F8F6E6" id="EmbedFlashFilesUpload" src="ClientSideControls/ElementITMultiPowUpload2.1.swf"
    quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"
    type="application/x-shockwave-flash" width="450" height="350" flashvars="uploadButtonVisible=false&uploadUrl=../ReceiveBulkCases.aspx&useExternalInterface=Yes">
  </embed>
</object>


<script>
  function updateMessage() {
    var message = (new Date()).toLocaleTimeString() + ': ';

    try {
      var objectElement = document.getElementsByTagName('object')[0];
      var embedElement = document.getElementsByTagName('embed')[0];

      if (objectElement.filesCount) {
        message += 'We are in IE ' + objectElement.filesCount();
      }
      else if (embedElement.filesCount) {
        message += 'We are in Firefox ' + embedElement.filesCount();
      }
      else {
        message += "The flash object is not loaded or useExternalInterface is not set to 'Yes'";
      }
    }
    catch (exp) {
      message += 'An error occurred';
    }
    document.getElementById('message').innerHTML = message;
  }

  // Update the message every 500 milliseconds
  setInterval(updateMessage, 500);
</script>
于 2009-05-08T08:14:41.143 回答