0

So, I'm having some trouble returning a value from an ExternalInterface call. I have a piece of code that looks like this:

var a:String = String(ExternalInterface.call("function() { var returnTest = 'test'; alert(returnTest); return returnTest;}"));
ExternalInterface.call("alert", a);

The first alert (in the anonymous function on line 1) is correct (obviously). However, the alert on line 2 is returning null 90% of the time in IE10. It works everytime in Firefox though.

To further explain the bit about working 90% of the time, it seems I can roll the dice again on whether or not it will work by adding or removing seemingly meaningless alerts. For example: let's say it's not working, I could add an alert and it will start working. Or, say it is working, I could add an alert for debugging, and it stops working, remove the alert, still doesn't work, add the alert back, and it starts working again. I know this isn't what's happening, but it's behaving as if a coin is flipped every time an alert is added or removed.

And this all only happens in IE, works perfectly every time in Firefox.

Thanks.

Edit: The code I provided isn't the actual code that needs to work, but rather the code that I wrote to verify where the problem was. The actual situation is that there's a JavaScript property in the environment our Flash is running in that we need to know, but we don't have access to the HTML or JavaScript the SWF will be running in. The actual code I need to run looks more like this:

var pageNameFromJS:String = String(ExternalInterface.call("function() { var pageName = ServerObject.currentPage.name; alert(pageName); return pageName;}"));
ExternalInterface.call("alert", pageNameFromJS);

The alert in the first line is just to make sure that ServerObject.currentPage.name works, which it does. The alert in the second line is debug code that was added when we noticed that functions that require pageNameFromJS weren't working.

Surgery
  • 217
  • 1
  • 3
  • 13
  • If you try it using something that doesn't hang the thread (eg not using alert but maybe appending the text of an html element) does it work 100% of the time? Problem might be that AS3 isn't waiting for the alert to be dismissed. Just a wild guess though – BadFeelingAboutThis Oct 28 '14 at 16:02
  • This is an unusual way to use ExternalInterface, at least I have not seen it performed this way. I am not sure why you are passing the response of the first line as the parameter of the second. If you are already making a call to JavaScript, then why not just execute everything there at once? What does your JavaScript function look like? – C. Parcell Oct 28 '14 at 16:02
  • I've edited the OP to better describe not only that problem but what I'm trying to accomplish. LDMS: It also seems to "flip the coin" if we made other changes, like changing the color of a TextField or something equally innocuous. – Surgery Oct 28 '14 at 19:46
  • I wonder if the second call is tripping a halt on recursive calls. Or could it be that the browser only allows one call to be triggered by user interaction? – C. Parcell Oct 29 '14 at 12:25
  • I'm not sure what you mean C. Parcell. I'm not seeing any recursion in my code, intention or unintentional. – Surgery Oct 29 '14 at 14:35

1 Answers1

0

Really I dont know why you complicate things like this ;)

You can do it easier :

AS3 code:

ExternalInterface.addCallback("flash_function", flash_function);

function flash_function(from_js_param){
    trace('param received from js : '+from_js_param)
}

ExternalInterface.call("js_function", to_js_param)

JS code:

function js_function(from_flash_param){
    var to_flash_param = 'I received your '+from_flash_param;
    (get_your_swf).flash_function(to_flash_param);
}
akmozo
  • 9,829
  • 3
  • 28
  • 44
  • Thanks, but I've added more info in the OP about why we can't do it this way. Basically, we have no access to the HTML or JavaScript being run in the environment. – Surgery Oct 28 '14 at 19:50
  • Ah OK, I see. May be you can give us some other informations about js code or html page ?! – akmozo Oct 28 '14 at 21:13
  • I'm not really sure there's much else to say that would be relevant. It's a custom Java-based webserver that generates HTML content from XML and serves it locally in it's own little environment, kind of like kiosk software. – Surgery Oct 29 '14 at 14:13