ExternalInterface

2019-08-21 19:00发布

问题:

Hey, so I'm having a bunch of trouble getting ExternalInterface to work, which is odd, because I use it somewhat often.

I'm hoping it's something I just missed because I've been looking at it too long.

The flash_ready function is correctly returning the objectID, and as far as I can tell, everything else is in order.

Unfortunately, when I run it, I get an error (varying by browser) telling me that basically document.getElementById(<movename>).test() is not a valid method.

Here's the code:

javascript:

function flash_ready(i){
    document.getElementById(i).test('success!'); 
}

Embed Html (Generated):

<script type="text/javascript">
    swfobject.embedSWF("/chainmaille/includes/media/flash/upload_image.swf", "/chainmaille/includes/media/flash/upload_image", "500", "50", "9.0.0","expressInstall.swf", {}, {allowScriptAccess:'always', wmode:'transparent'},{id:'uploader_flash',name:'uploader_flash'});
</script>
<object type="application/x-shockwave-flash" id="uploader_flash" name="uploader_flash" data="/chainmaille/includes/media/flash/upload_image.swf" width="500" height="50"><param name="allowScriptAccess" value="always"><param name="wmode" value="transparent"></object>

AS3 :

package com.jesseditson.uploader {

  import flash.display.MovieClip;
  import flash.external.ExternalInterface;
  import flash.system.Security;

    public class UI extends MovieClip {

     // Initialization:
     public function UI() {
        Security.allowDomain('*');
        ExternalInterface.addCallback("test", test);

        var jscommand:String = "flash_ready('"+ExternalInterface.objectID+"');";
        var url:URLRequest = new URLRequest("javascript:" + jscommand + " void(0);");
        navigateToURL(url, "_self");
    }

    public function test(t){
      trace(t);
    }
  }
}

Swfobject is being included via google code, and the flash embeds just fine, so that's not the problem.

I've got a very similar setup working on another server, but can't seem to get it working on this one. It's a Hostgator shared server. Could it be the server's fault? Anybody see any obvious syntax problems?

Thanks in advance!

回答1:

The Flash isn't actually finished constructing yet. You're calling your flash_ready callback from the constructor, and so the JS is trying to call in before the object is on the stage. Move your flash_ready call to, say, an Event.ADDED_TO_STAGE listener and it should start working.



回答2:

Ok, so after further investigation, it appears that there was a problem with multiple instances of the flash object, because I was cloning it to a lightbox. I have gotten it working now.



回答3:

Just spent 5 hours fighting with this. It was all really frustrating. In my case the solution was very very simple and I would have never guessed it. So first of for all of you who have never seen the flash player debugger running on files inside your browser finding this link that walks you through the setup has resulted in a somewhat magical experience :) Now to my ExternalInterface discovery: There was a sandbox violation inside the same domain. This meant that flash cannot access www.yourdomain.com from yourdomain.com. The weird thing of course is that you are not explicitly calling the domain with ExternalInterface. Anyhow, the solution was very simple: add this to my .htaccess file and that was it!

# Redirect non-www to www 
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.youtdomain.com/$1 [R=301,L] 

Hope this helps someone



回答4:

Is flash_ready receiving the objectID that you are expecting?

Also, why use:

var jscommand:String = "flash_ready('"+ExternalInterface.objectID+"');";
var url:URLRequest = new URLRequest("javascript:" + jscommand + " void(0);");
navigateToURL(url, "_self");

When this is designed to do that:

ExternalInterface.call("flash_ready", ExternalInterface.objectID);


回答5:

Post your source (pre-generatred). When your browser says [Flash].Method is not a function, 99% of the time it means that you tried calling a function in Flash before the .swf was ready for it. I see that you tried to do that, but clearly its not working so something must be off. So post your pre-rendered source and I am sure we can find the problem.