Flash ActionScript – getURL in AS3

Flash ActionScript

After a long time I am writing something…during my project I found out something interesting in Flash and JavaScript function. This will be useful for people with basic knowledge in Actionscript. So let’s discuss it here.

First we will discuss about the difference between AS2 and AS3 script on Flash button. Previously in AS2 it’s very easy to write script on Flash button.

AS2 script on Flash button:

on(release){

//Write your action

trace(“Button has been pressed”);

}

And function should be like this (this should be placed on the keyframe of timeline (make actionscript layer) :

function myFunction(){

//Write your action

trace(“Hello”); //write trace option

}

Above example was quiet easy with AS2, now let’s see same example with AS3:

Important point to remember is that in AS3 you can’t write script on button. Just write it on keyframe in timeline (make actionscript layer). And give instance name to your button e.g. HelloButton. Now write the button script on keyframe of timeline:

AS3 script for Flash button:

HelloButton.addEventListener(MouseEvent.CLICK, myFunction);

Here, we have use button name i.e. HelloButton. And a function i.e. myFunction

In AS3 function is written as:

function myFunction (event:MouseEvent):void{

                trace(“Hello”);

}

In above function, remember to add “event:MouseEvent” and “:void” as compare to AS2, which tells that on which mouse event this function should execute.

Now let’s discuss about simple script of “getURL”. Previously it was easy to open a webpage on click of a Flash button. You just need to put this script on Flash button. See example:

on(release){

                //getURL(“http://www.google.com”, _blank);

                }

After clicking this button it will open an http://www.google.com link in new window.

But now if you just put this script on Flash button and publish HTML, you will find that it’s not working….don’t worry this is just a simple glitch within your HTML…….try to find out this param tag…in HTML:

<param name=”allowScriptAccess” value=”always”/>

Make sure that….value=”always”….if you want to run getURL script on Flash button….Now your Flash button works perfectly in HTML.

Now learn something interesting, capture Flash button event in Javascript of HTML page. Check AS2 and AS3 versions:

AS2 version:

On Flash button write this script:

on(release){

               getURL(“javascript:myfunction()”);           

}

In above script, “myFunction()” is a javascript function which is present in HTML.

In HTML, where you are going to run this Flash button, just write javascript function “myFunction()” in <body> section. Check this:

JavaScript Function in HTML

<script language=”JavaScript”>

function myfunction(){

alert(“Hello World”);       

}

In the object tag for the SWF file in the containing HTML page, set the following parameter:

<param name=”allowScriptAccess” value=”always” />

Now check above scenario in Actionscript 3:

AS3 version:

import flash.external.ExternalInterface;

Instead of “getURL”, in AS3 we should use “ExternalInterface” classes. For more details check link: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html

import flash.events.Event;

HelloButton.addEventListener(MouseEvent.CLICK, myFlashFunction);

Above code is for Flash button with instance name: “HelloButton”, and when someone press this button it execute “myFlashFunction”(JavaScript function).

 

function myFlashFunction (e:Event):void{

     ExternalInterface.call(“myfunction”, HelloButton.name);

};

myFlashFunction is a Flash function which runs javascript function(i.e. myfunction) in HTML file.

 

And in HTML, place this javascript function:

<script language=”JavaScript”>

function myfunction(){

alert(“Hello World”);       

}

In the object tag for the SWF file in the containing HTML page, set the following parameter:

<param name=”allowScriptAccess” value=”always” />

 

Now run HTML file in Internet Explorer (browser). And when you press Flash button in SWF file in the containing HTML page, javascript code catch the Flash button event. This way you can catch any Flash event in javascript and use for many purposes. Hope this will be helpful for those who want to understand basic event catching with JavaScript from Flash.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Copy link
Powered by Social Snap