views:

1750

answers:

2

My simple ActionScript I am trying to use Flash's ExternalInterface to setup a callback so that JavaScript can call a method on my Flash object. Everything works fine in Safari, Firefox and in IE, but I cannot get Chrome working. When I try the code on Chrome, I get the following error:

Uncaught TypeError: Object #<an HTMLObjectElement> has no method 'setText'

To see it in action, I have an example up and running.

Here is the example HTML I am using (again, works fine in Safari, FF and IE)

<html><body>
<div id="mycontent"></div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"&gt;&lt;/script&gt;
<script type="text/javascript">
swfobject.embedSWF("http://invincible.dynalias.com:8080/HelloWorld.swf", "mycontent", "400", "420", "9.0.0","expressInstall.swf", {}, {allowScriptAccess:'always'},{id:'hw',name:'hw'});

function getFlash(movieName) {
   return ( navigator.appName.indexOf("Microsoft") != -1) ? window[movieName] : document.getElementById(movieName);
}
</script><p>
  <input type="text" id="exampleText" /> <input type="button" value="Set Text" onclick="getFlash('hw').setText(document.getElementById('exampleText')
.value)" />
</body>
</html>

and here is the ActionScript...

package {
  import flash.display.Sprite;
  import flash.text.TextField;
  import flash.external.ExternalInterface;
  import flash.system.Security;

  public class HelloWorld extends Sprite {

    private var textField:TextField = new TextField();
    public function HelloWorld() {
      Security.allowDomain("*");
      ExternalInterface.addCallback("setText", this.setText);
      textField.text = "Hello, world!";
      addChild(textField);
    }   
    public function setText(text:String):void {
      this.textField.text = text;
    }   
  }
}
A: 

I would file a ticket with Chromium if I were you. This looks like some platform dependent (works on my Mac Chrome but not on Windows one) issue where setText method does not get propagated to HTMLObjectElement.

It could also be caused by swfobject, so you it might make sense to ask on a forum/list dedicated to that script support.

kangax
+1  A: 

I was having problems with ExternalInterface and Firefox and Chrome and discovered that the Adobe Script was not writing the Flash tag quickly enough, so when the browser tried to find the addCallback() function it was not there at the time.

Simply putting my Javascript function that calls the Flash created addCallback() in a window.setTimeout() calling solves the problem. Delays less than 200 ms still make the problem to occur.

I didn’t have to use the solution of trying to find if the “length” attribute exists in the document[FlashId] object. Just calling “FlashEmbed = document[FlashId]” worked just fine.

Robson Waterkemper
Interesting, I'll have to try that
Rob Di Marco