FORUMS: list search recent posts

calling JavaScript from ActionScript 3

COW Forums : Adobe Flash

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
demetri tashie
calling JavaScript from ActionScript 3
on Feb 24, 2009 at 2:46:21 am

i have been kicking myself in the head over this one for a while. i have read tons on the web about it, but have never found any satisfying, realistic answer or explanation on how to trigger a javascript script from a Flash event. Adobe says to use the ExternalInterface class, but it gives real confusing info.

Has anybody done this? Can we distill it to its most basic possibility? My proposed scenario that i think we could all learn from would be this: to have a MovieClip with the Instance name "myMC" in a SWF that is in your HTML page that has a javascript function called " myJavaScriptFunction " which triggers a Javascript alert.

i would assume the ActionScriopt 3 code to be:

import flash.external.ExternalInterface;

myMC.addEventListener(MouseEvent.CLICK , myAS3fuction );
function myAS3function(evt:MouseEvent):void {
ExternalInterface.call("myJavaScriptFunction");


and the javascript in the HTML document to be:

..script type="text/javascript" ...
function myJavaScriptFunction (message){
alert(message)
}
myJavaScriptFunction('this alert works !');
.../script...


am i doing something wrong ??

thanks for any help.





Return to posts index

Leandro Silva
Re: calling JavaScript from ActionScript 3
on Feb 6, 2010 at 6:07:57 am

Hey Demetri, you should not be calling your function inside the html (since you're calling from flash). That is why your param is not passing.

Best

Leandro Silva | grafluxe.com


Return to posts index

demetri tashie
Re: calling JavaScript from ActionScript 3
on Feb 6, 2010 at 2:09:18 pm

hey Leandro-

thanks for writing in . this was an old post - about a year ago! - figured it out since then : - )

for anyone who is interested, here is the complete code ( note: there was a typo in the original AS )

AS3 code:

import flash.external.ExternalInterface;

myMC.addEventListener(MouseEvent.CLICK , myAS3function );
function myAS3function(evt:MouseEvent):void {
ExternalInterface.call("myJavaScriptFunction"); }


and the HTML code:

<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
function myJavaScriptFunction (message){
alert('this alert works !')
}
</SCRIPT>


hope that helps someone else not struggle the way i did !

just to sum up what is going on - it is getting a click of a MovieClip symbol in a SWF in a HTML page to trigger a JavaScript alert - using the infamous "External Interface"


Return to posts index


Leandro Silva
Re: calling JavaScript from ActionScript 3
on Feb 10, 2010 at 3:45:56 am

I paid attention to the month and day but disregarded the year. I responded just about a year late.. sorry about that.

Glad to hear you solved your issue.

Also, for anyone else:

To pass a parameter from flash to JS, do the following:

ExternalInterface.call("myJavaScriptFunction", param1, param2, etc); }



Leandro Silva | grafluxe.com


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]