View comments | RSS feed

asfunction protocol

asfunction:function:Function, parameter:String

A special protocol for URLs in HTML text fields that allows an HREF link to call an ActionScript function. In HTML text fields, you can create links using the HTML A tag. The HREF attribute of the A tag contains a URL that uses a standard protocol such as HTTP, HTTPS, or FTP. The asfunction protocol is an additional protocol that is specific to Flash, which causes the link to invoke an ActionScript function.

Availability: ActionScript 1.0; Flash Player 5

Parameters

function:String - An identifier for a function.

parameter:String - A string that is passed to the function named in the function parameter.

Example

In the following example, the playMP3() function is defined. The TextField object list_txt is created and set so HTML text can be rendered. The text Track 1 and Track 2 are links inside the text field. The playMP3() function is called when the user clicks either link and plays the MP3 that is passed as a parameter of the asfunction call.

var myMP3:Sound = new Sound();
function playMP3(mp3:String) {
 myMP3.loadSound(mp3, true);
 myMP3.onLoad = function(success) {
 if (!success) {
 // code to handle errors here
 }
 };
}
this.createTextField("list_txt", this.getNextHighestDepth(), 0, 0, 200, 100);
list_txt.autoSize = true;
list_txt.html = true;
list_txt.multiline = true;
list_txt.htmlText = "<a href=\"asfunction:playMP3, track1.mp3\">Track 1</a><br>";
list_txt.htmlText += "<a href=\"asfunction:playMP3, track2.mp3\">Track 2</a><br>";

When you click a link, the MP3 sound file streams in Flash Player.

See also

htmlText (TextField.htmlText property)


Version 8

Comments


No screen name said on Oct 14, 2005 at 6:21 AM :
Is it possible to include a full target path to the function. For example:
<a href="asfunction:_global.MainObj.PageArea.displayTellFriend">Click</
a> In order to call a method 'displayTellAFriend'? How is the string
evalutated?
howlongis said on Oct 19, 2005 at 6:23 AM :
_root.path.to.myFunction, that works fine.
_global.path.to.myFunction also fine.
Relative paths work, relative to text field's parent movie clip.
I was testing using AS2, player 8 - easy enough to test this for yourself in
your target platform.
JimmyMUK said on Oct 24, 2005 at 4:53 PM :
I can see the need for a slight enhancement to this function. I for instance want to handle external links with a function so that the user can see they will be going to an external site after clicking on a link, so I embed HTML such as this:

<a href="asfunction:_root.urlLink, http://www.macromedia.com">

My urlLink function will then open Macromedia's page.

However if the user right-clicks on the link and chooses "Copy Link" the internal AS code "asfunction:_root.urlLink, http://www.macromedia.com" is copied into their clipboard.

It would be nice if there was some sort of addition to asfunction to specify a copy text - e.g. in this case I'd want it to copy just "http://www.macromedia.com" to the clipboard.
cosmits said on Nov 15, 2005 at 2:06 AM :
Is it possible to pass more then one parameter to the function which is being called ?
or should i pass an array of parameters as a single parameter ?
thank you for your time
Yariv :)
alienorbit said on Dec 1, 2005 at 12:47 AM :
Wen using external text files to populate the text field in Flash 8, it seems the link must be formatted with single quotes - escaped double quotes do not work.

<code><ahref=\"asfunction:myFunction, myVar\">Track 2</a></code>

does not work, while

<code><a href='asfunction:myFunction, myVar'>Track 2</a></code>

does work. Granted I'm no coder, so there's a million things I could've done wrong - but in my particular setup - the latter works, while the former does not.
Marcílio-BSB said on Dec 9, 2005 at 4:21 AM :
Ok, thats great! But if my function has 2 or more parameters, what do we do? I'm trying but had no success.
SimpleScripter said on Jan 1, 2006 at 5:47 PM :
Marcílio,
One way to pass multiple parameters would be to use a comma to separate each new value. Then String.split the parameter into individual values in the called function. This is a simple method that would only work with string values.
artfabrique said on Jul 18, 2006 at 10:24 PM :
May be a BUG!

I have :
1)html textfield with a href asfunction call
3)simple symbol - type:button

After clicking on this link in te dynamic textfield all button stop working.
Troubles in Flash 8 Professional.

Here is an example of this bug www.artfabrique.ru/Untitled-4.rar

Open it and try this:
1)Ctrl+Enter (Test movie)
2)click on the square button named "1"
3)Now you see attached movieclip
4)Click on the text link ("Click ME Click ME")
5)now try clikin' on the button "BUG" (that is on the left side of text link)
6)In the out put window yo can se how many times you've clicked it.
7) as you can see "on(Press) { } " event doesn't work proper after using asfunction call.. AND it is happening whith all buttons!!!!
NPaquin said on Aug 29, 2006 at 7:41 AM :
Can asfunction protocol be used within a TextArea component ?
simonwheatley said on Oct 1, 2006 at 5:07 AM :
Beware: for Flash v8, the documentation suggests: "asfunction:_root.myFunction, myParam". However the param passed includes the space after the comma, i.e. what is passed is " myParam" (note the leading space).
mgalv said on Dec 7, 2006 at 8:26 AM :
Experienced that spaces after the comma –as in the example– will cause unexpected behaviour; the argument passed will arrive with a preceeding space:
<pre>var myId = 5;
var myHtmlText = "<a href='asfunction:myFunc, "+myId+"'>click here</a>";
var myObj = {};
myObj['5'] = "this is right";
myObj[' 5'] = "this is rong";
function myFunc (str) {
trace (myObj[str]);
}</pre>
Crushed Pineapple said on Jan 2, 2007 at 10:05 AM :
Because I am using dynamic XML text that displays the actual link path, e.g., "http://www.adobe.com", is there any way to use a variable as the text to be clicked, instead of having to hardcode the text? In the above sample, instead of coding "Track 2" I'd like to display a variable that has the value of "http://www.adobe.com".
No screen name said on Jan 3, 2007 at 6:56 PM :
Did some experimenting with this...
The second parameter seems to only be a string. So, if you want multiple paramerts you'd have to define an array elsewhere, then in your function you call with asfunction make the 1 parameter a string and then use brackets to evaluate the string...

var myArr:Array = Array('val1', 'val2');

function multiParameter(arrayName:String) {
var myParameters:Array = this[arrayName];
}

var content:String = '<a href="asfunction:multiParameter,myArr">Here's the content for your html textbox</a>';

*note: The "myArr" in the asfunction command has no space before it. If you add a space, multiParameter will look for a array called " myArr" and not find it. This is where I was having problems. :-L
No screen name said on Mar 26, 2007 at 8:48 AM :
Avoid the use of space between the comma and the parameter:

function gotoFrame(someFrame) {
gotoAndStop(someFrame);
}

<a href="asfunction: gotoFrame, intro">GO TO INTRO</a>

This wont work if your framelabel is called "intro", because of the space. In the example given in the Help a space is used, because the code look better, I guess... But it wont work in all situations.
snowpoi said on Apr 21, 2007 at 10:13 AM :
I've been trying to find a workaround for making asfunction react to
mouseOver/rollOver in a text field, but I couldn't succeed yet. Is that
possible anyway?
shanstafari said on Apr 24, 2007 at 3:55 PM :
One problem I noticed: if my parameter is a string (surrounded by quotes), the whole thing breaks, and the text disappears... very odd.
No screen name said on Apr 25, 2007 at 11:10 AM :
How do you remove the text "asfunction" when a user right clicks and selects copy link?
No screen name said on Jul 21, 2007 at 1:33 PM :
Is there a way to make html within a TextField <a href='asfunction:gotoAndStop,3'>whatever</a> use a "onRollOver to frame 3" and "RollOut frame 2" then when the user clicks on the link it takes them to a URL?

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00001719.html