14Mar
[AS 3] Calling Javascript Functions without FSCommand
10 comments so farHere 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);
}
}
}
}
Categories: Actionscript 3, Flash and Javascript
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.
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?
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.
I undstand all the flash and acionscript stuff, but where do you locate your javascrpt function?
NickEBy03@aol.com email me
The javascript function for this example is placed within your html page between the
tags. So it would be like:10x a lot
am making a registration form in flash, so i needed this to leave a cookie
and how to do this, with a Class but upside down? from JS call a AS3 Class function?
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?
Can you provide more details of your issue?
Morgan
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?
Hi Andrey,
I just tested this page in IE6 PC running Win 2000 without any issues.
Morgan