In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

AS - JS

ASからJSは呼べるけどJSからASが呼べない.
http://jsdo.it/cpu_t/fuHy/
Get Adobe Flash player
by cpu_t 20 Jun 2010

    Talk

    bkzen at 21 Jun 2010 03:23
    Security.allowDomain("*", "jsdo.it"); とか入れておけば大丈夫。JS側はallowScriptAccessをalwaysにしておく
    Embed
/**
 * Copyright cpu_t ( http://wonderfl.net/user/cpu_t )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/kTU4
 */

// ASからJSは呼べるけどJSからASが呼べない.
// http://jsdo.it/cpu_t/fuHy/
package
{
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.external.ExternalInterface;
    import flash.events.MouseEvent;
    public class FlashTest extends Sprite
    {
        public function FlashTest()
        {
        		this.x=10;this.y=10;
        		
       		var sendBtn:Sprite=new Sprite();
       		addChild(sendBtn);
       		var tf:TextField=new TextField();
       		sendBtn.addChild(tf);
       		tf.text="callJS";
       		tf.autoSize="left";
       		tf.selectable=false;
       		sendBtn.graphics.beginFill(0xD0D0D0);
       		sendBtn.graphics.drawRect(0,0,tf.width,tf.height);
        		sendBtn.addEventListener(MouseEvent.MOUSE_DOWN,buttonHandler);
        		
        		input=new TextField();
        		addChild(input);
        		input.type="input";
        		input.autoSize="none";
        		input.height=input.textHeight+4;
        		input.border=true;
        		input.borderColor=0;
        		input.x=sendBtn.width+2;
        		
        		output=new TextField();
        		addChild(output);
        		output.y=input.height+2;
        		output.text="output area\n";
        		output.autoSize="left";
        		
        		// JavaScriptからは"callAS"で呼び出す
        		ExternalInterface.addCallback("callSideAS",callSample);
        }
        private var input:TextField;
        private var output:TextField;
        private function callSample():void
        {
        		output.appendText("do AS\n");
        }
        
        private function buttonHandler(e:MouseEvent):void
        {
        		var msg:String=input.text;
        		input.text="";
        		
        		// ActionScriptからJavaScriptを呼び出す
        		try
        		{
        			var result:String=ExternalInterface.call("callSideJS",msg).toString();
        			output.appendText(result+"\n");
        		}
        		catch(e:Error)
        		{
        			output.appendText(e.toString()+"\n");
        		}
        }
    }
}