14Mar

[AS 3] Calling Javascript Functions without FSCommand

10 comments so far

Here is a super simplified version to call a javascript function that is accessible by your html page. If you type some text in the input field below and then click the button, it will trigger a javascript alert box. Remember, this is just a very simplistic snippet of code that doesn’t do any checking or validating of the page loading sequence or when the javascript functions are accessible by the page.

Here is my basic Javascript function:

function jsAlert(msg)
{
   	alert(msg);
}

Here is the Document Class:

package {
    import flash.display.Sprite;
    import flash.events.MouseEvent;
	import fl.controls.Button;
    import flash.external.ExternalInterface;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.text.TextFieldAutoSize;

    public class CallingJavascriptFunction extends Sprite {
        private var input:TextField;
        private var button:Button;

        public function CallingJavascriptFunction() {
            input = new TextField();
            input.type = TextFieldType.INPUT;
            input.background = true;
            input.border = true;
            input.width = 300;
            input.height = 18;
            addChild(input);

            button = new Button();
            button.width = 250;
            button.height = 30;
            button.move ( 320, 0 );
            button.label = "Call Javascript Alert";
            addChild(button);
			button.addEventListener(MouseEvent.CLICK, buttonClick);
        }

        private function buttonClick(m:MouseEvent):void {
            if (ExternalInterface.available) {
				// jsAlert is a function in the html page
                ExternalInterface.call("jsAlert", input.text);
            }
        }
    }
}

Share/Save/Bookmark

Friday, March 14th, 2008 at 10:09 am and is filed under Actionscript 3, Flash and Javascript. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

10 Responses to “[AS 3] Calling Javascript Functions without FSCommand”

  1. Posted by Wino 4th April, 2008 at 6:54 am

    Thanks for this. I’m going to look it up, but what is the purpose of the check for EI availability? Does this have to do with asynchronous loading of html and swf code?

    How can I insure a call is made using this? Is there an event I can tie the EI call to, rather than the mouse.CLICK?

  2. Posted by admin 4th April, 2008 at 10:30 am

    I agree that the EI check is for the asynchronous page load of elements. I can’t think of any events that to tie the EI call to.. one work around would be to use the timer and check for EI availability.

  3. Posted by nick 8th May, 2008 at 7:20 am

    I undstand all the flash and acionscript stuff, but where do you locate your javascrpt function?
    NickEBy03@aol.com email me

  4. Posted by admin 8th May, 2008 at 8:11 am

    The javascript function for this example is placed within your html page between the tags. So it would be like:

    
    

  5. Posted by mit 21st August, 2008 at 10:47 pm

    10x a lot

    am making a registration form in flash, so i needed this to leave a cookie

  6. Posted by lito 1st September, 2008 at 4:18 am

    and how to do this, with a Class but upside down? from JS call a AS3 Class function?

  7. Posted by Rad 14th October, 2008 at 7:46 pm

    I am a total newbie at this. but I find that this function is very useful for the site i’m currently developing.. so can anyone teach me how to use this? :) please?

  8. Posted by admin 15th October, 2008 at 4:35 am

    Can you provide more details of your issue?

    Morgan

  9. Posted by Andrey 24th October, 2008 at 11:39 am

    Great article. It really helped me. But I have some problems with Internet Explorer 6. This code works great in Opera and FireFox and Explorer returns javascript error (in function jsAlert(msg) I have only alert call).

    Is this only my problem or not?

  10. Posted by admin 24th October, 2008 at 12:23 pm

    Hi Andrey,

    I just tested this page in IE6 PC running Win 2000 without any issues.

    Morgan

Leave a reply