Calling Javascript Functions From Flash

Tonight I will be giving you a very short tutorial on how to call javascript functions from your Flash movies. This is a very simple process, and if you follow along you’ll be scripting in no time. The following tutorial is going to be a precursor to an upcoming post detailing how to execute my Lightbox++ script from your Flash movies.

Example


Upon clicking the button labeled “Click Here” in the above Flash video, you will be greeted with a javascript alert. The Flash video contains a button when pressed triggers an event that calls a function within this page that triggers the alert. At this point, you can view the source of this page and if you look close enough, you’ll find a function labeled “myfunction.” You can create functions that can do anything you can imagine, but for the sake of this example we’re just using a very basic javascript alert.

Cool, How Do I Do It?

First you’re going to need to put your javascript function between the head tags of your document. For this example, I’ve used the following function:

1
2
3
4
5
<script language="javascript">
function myfunction() {
   alert("This function was successfully triggered via the flash movie.");
}
</script>

Moving on, you’re going to need to make a small Flash video that contains either a button, or something else that can trigger events. If you’ve already previously built something, and you want to make javascript calls from your project, that is fine too. Within the Flash event in which you wish to trigger your javascript function, you’re going to be using the getURL command, to issue the call. Mine looks something like this:

1
getURL("javascript:myfunction();");

This should pretty clear what was done here. You’re prefixing your function name in your getURL command with “javascript:” to let the browser know you intend to call a bit of script. After that, you’re putting in your function name that we’ve included in the head tags of our document earlier. If your javascript function accepts arguments, then these must be specified in your getURL command as well.

Now you should embed your Flash movie into your document that contains your javascript, and give it a whirl. It should be noted that for this to work properly, you’re going to need to upload your document and your Flash movie to your hosting service.

Well, that wasn’t too bad now was it? Enjoy your new found skills, and look for tomorrow’s post on how to execute my Lightbox++ and many other similar scripts from your Flash movies.

Download Example



If you wish to download an example for this post, you can do so here - Download Example Share This

Leave a comment

Please be polite and on topic. Your e-mail will never be published.