开发者

How do I replace a URLLoader call with an ExternalInterface call?

We have been given some code that does a URLRequest call which is really slow to respond, so I was lookin开发者_JAVA百科g to make it pull the data from the page, as it is already on the page.

//what we have
_dataLoader = new URLLoader();
_dataLoader.addEventListener(Event.COMPLETE, _eventTriggered);
_dataLoader.load(new URLRequest("http://somesite.com/xmlstuff.xml"));

I have tried to replace it with this (actionscript is definitely not my forte):

_dataLoader = new ExternalInterface();
_dataLoader.addCallback(_eventTriggered);
_dataLoader.call('some_function_returning_xml');

I am using addCallBack as that is what the editor suggested via autocomplete, unfortunately it doesn't seem to work. Unfortunately I cannot go back to the developer at this time.

The error message is:

Call to a possibly undefined method addCallback through a reference with static type flash.external:ExternalInterface


ExternalInterface is a static class. So you shouldn't create a new instance of it. Here's the fix:

ExternalInterface.addCallback(_eventTriggered);
ExternalInterface.call('some_function_returning_xml');

Additionally, the method you "call" can return a String back immediately so you don't need to add a callback. Just return the XML as a String in Javascript and you get it as a String in AS3. Full Example:

// JS
function some_function_returning_xml(){ return "<result>Sweet</result>"; }

// AS3
var xmlStr:String = ExternalInterface.call('some_function_returning_xml');
var xmlData:XML = new XML(xmlStr);

ExternalInterface Docs: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html

Enjoy!


You don't use ExternalInterface() as an object that you 'new'. Instead it's a static class with some exposed functions. Try this:


ExternalInterface.addCallback("some_function_returning_xml", _eventTriggered);


ExternalInterface is a class that makes it possible to call functions in the containing environment, and as the other answers state, it's a static class.

You probably want something like this:

if(ExternalInterface.available)
{
    ExternalInterface.call("your_javascript_function_name");
}

Or if you can even inject a javascript function and execute it immediately, by writing the javascript in the String:

if(ExternalInterface.available)
{
    ExternalInterface.call("function(){alert('it works!');};");
}

The ExternalInterface.addCallback method allows you to expose an ActionScript function to the containing environment - so you could have JavaScript functions call ActionScript functions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜