views:

3985

answers:

8

I have a Javascript function that returns the innerHTML of a div. I am attempting to call this function from Actionscript and store the return value. I know that the Javascript function is being called because there is an alert that displays the return data, The data that is returned to Actionscript, however, is null. I am not sure what is causing this. Here is a code example of what I am attempting to do:

Javascript:
function JSFunc () {
     var x = document.getElementById("myDiv");
     alert(x.innerHTML);
     return x.innerHTML;
}

Actionscript:
import flash.external.*;
if (ExternalInterface.available) {
     var retData:Object = ExternalInterface.call("JSFunc");
     if(retData != null) {
          textField.text = retData.toString();
     } else {
          textField.text = "Returned Null";
     }
} else {
     textField.text = "External Interface not available";
}

Like I said earlier, the alert shows up with the contents of the div but the text in the textfield is always "Returned Null", meaning that the ExternalInterface is available. I should add that I can only test this in IE7 and IE8. Any advice on what to do would be much appreciated.

A: 

You should use the import statement

import flash.external.*;
Claudio
Sorry forgot to mention that I was already doing that.
Adam Richardson
A: 

Try adding as String to the call:

textField.text = ExternalInterface.call("JSFunc") as String;
Simon Lehmann
Gave typecasting a shot but no dice.
Adam Richardson
A: 

I also noticed you got a typo in your code => textField != textfield

Claudio
Sorry for the mistake the code is an example not what I am actually using.
Adam Richardson
Well, there's nothing wrong with that code, unless some other part of your code is messing with up with the textfield content.
Claudio
A: 

Try to get the object back from your external interface call without casting it first, and take a look at it in the debugger. If it is not a string, trying to cast it to a string will result in null. This should be a string, but it doesn't hurt to see what you are actually getting back before you try to work with it.

Osman
The object is null, is what the debugger is telling me.
Adam Richardson
+1  A: 

This is a working sample based on the code you provided. You can right click it to view the source. I suspect the problem lies in the HTML for 'myDiv' or when you are making the actionscript call.

Osman
Thank you for showing that the code itself is not the problem, I am thinking that the issue might be with the object tag that I used to embed the movie in IE. Also how to do I see the javascript and the div that you used?
Adam Richardson
nm, I found the javascript and div.
Adam Richardson
+1  A: 

The source of the problem that I have been having has to do the object tag that I was using to embed the flash movie. I was using a tag that followed this example http://www.w3schools.com/flash/flash_inhtml.asp, I changed it to match this example: http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_4150 and then I made sure that I added id to the object and everything worked.

Adam Richardson
A: 

It seems to me that your problem is that from javascript you are returning a string, the innerHTML property of your myDiv element. In actionscript you have datatyped the variable that the ExternalInterface call returns to as an Object, but it is a String. Maybe you have already caught this, but I can't tell as you haven't amended your code.

//The following is an Object
var x = document.getElementById("myDiv");

/*
You are returning the innerHTML property of x, a string, but on the
flash end your expecting an object in your actionscript.
*/
return x.innerHTML;

//The following seems incorrect to me.
var retData:Object = ExternalInterface.call("JSFunc");

//Should be
var retData:String = ExternalInterface.call("JSFunc");

Hope this was helpful, take care.
Brian Hodge blog.hodgedev.com

Brian Hodge
A: 

Hi,

I'm facing a similar issue. And the following link doesn't seem to work. http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_4150

Can someone please let me know what the fix is?

Thanks, Sushma

The important thing that I found on that link was the official way to embed the flash swf inside so that it works with as many browsers as possible (This is different than the w3schools tag). The main thing that my embed tag was lacking is the following.OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"Google flash embed to find the rest of the tag.
Adam Richardson