How to Trigger ActionScript with Text Field Hyperlinks (AS2)

Flash ActionScript 2.0

Flash makes it very easy to turn ordinary text fields into hyperlinks. Sure, it can be done programmatically with dynamic text fields and ActionScript, but it can also be done with plain vanilla static text fields:  just select a text field (or words in it) and enter the desired URL into the Property inspector.  It’s the field at bottom center of that panel — not labeled, but you’ll see “URL link” in a tooltip when you hover over it.

What isn’t immediately obvious is that you can also trigger functions with that URL link field.  Why would you?  Well, maybe you want something to spin around on the Stage before your hyperlink opens a URL, or heck, maybe you don’t want to visit a URL at all, but instead, send the timeline to another location.  Here’s how to do it. 

An answer, short and sweet

Use the asfunction protocol rather than http.  Select your text field and type the following into the URL link of the Property inspector:

asfunction:trace,Hello world!

When you test your SWF, click the text field to send the string “Hello world!” to the Output panel.  Simple as that.

Note:  Under normal circumstances, you would pass the string parameter “Hello world!” into a set of parentheses (()) after the function name — but here, no parentheses are present, and the string parameter is not quoted.  Of course, you can use the above technique with functions of your own, not just native functions like trace().

asfunction:gotoAndPlay,25
asfunction:myFunction,some parameter

How it works

The key is the asfunction protocol.  In computing, putting it very simply, a protocol governs the communication of data.  The Internet protocol suite, for example, defines dozens of them, including the well known http, ftp, and mailto.  ActionScript simply happens to support a proprietary protocol for triggering itself.

Unfortunately, asfunction it seems a bit limited.  I can only get it to accept one parameter after the function name … anything after that first comma, including other commas, seems to get included in the parameter.  But hey, at least it does something!  ;)

8 Responses to “How to Trigger ActionScript with Text Field Hyperlinks (AS2)”

  1. NSurveyor Says:

    When I use it and I need multiple parameters, I would define a custom function that would split the only parameter on commas, giving me essentially my arguments in an array. For example:

    function makeSomethingHappen(info:String) {
      var args = info.split(',');
      var what:String = args[0];
      var when:Date = new Date(Number(args[1]));
      var where:MovieClip = eval(args[2]);
      if (new Date()>when) {
        where[what]();
      }
    }
    stop();
    "asfunction:makeSomethingHappen,play,234,_root"

    will make the root play when you click on it, because 234 represents Dec 31, 1969, which has alrady passed.

    "asfunction:makeSomethingHappen,play,2151190071921,_root"

    will NOT make the root play when you click on it, because 2151190071921 represents Mar 2, 2038 which has not occurred yet.

    Though this doesn’t work well when one of your “parameters” is a string with commas in it… I guess you could choose another character for the delimeter.

    Also, you can use asfunction in a textfile, and then load it into a html enabled textfield. Something like:

    <a href="_root.unloadMovie" rel="nofollow">Bye bye!</a>

    inside the text doc, myText.txt

    Then in flash, something like:

    my_lv = new LoadVars();
    my_lv.onData = function(r){
    my_txt.html = true;
    my_txt.htmlText = r;
    }
    my_lv.load("myText.txt");
  2. David Stiller Says:

    Good thinking, NSurveyor! It’s funny, as soon as I published that one, the String.split() method occurred to me. But I didn’t log back in to update the article. Glad you posted your thoughts. :) Some of my best insights come to me when I finally stop thinking about it.

  3. Toby Miller Says:

    This is great, I really appreciate the article … it saved my ass this morning. =)

  4. David Stiller Says:

    Glad to hear it, Toby! :)

  5. Jase Says:

    Any idea about how this could be used with rollover events in text. I would like to be able to dynamically size my textfield which contains a bunch of words that when moused over should pop-up a MovieClip. Right now I place invisible buttons behind the text, but there has got a be a better way.

    Any advice would be great. Thanks.

    jase

  6. David Stiller Says:

    Jase,

    For better or worse, hyperlinks in Flash’s version of HTML only respond to the equivalent of a Button.onRelease event. It’s possible to style hyperlinks with CSS, so that they visually respond to rollovers, but those “quasi-event handlers” can’t be tasked with actually doing anything.

    Several months ago, I read an entry in the Adobe ActionScript forum (by kglad, I think) that suggested using movie clips in place of the hyperlinked words. That is, when you would normally have entered an <a> tag, use an <img> tag instead, and set the src attribute of that tag to the Linkage identifier of a movie clip in your Library. That movie clip would simply be a symbol that wraps a very small text field with the desired word. An id attribute in the same <img> tag will allow you to assign an instance name to the movie clip in question.

    I haven’t toyed with the concept myself, but it’s probably worth a shot.

  7. mike Says:

    have you toyed with that tag solution yet? i’m lacking a little knowledge on that techinique and could use some insight into how to do it.

  8. David Stiller Says:

    mike,

    I have toyed with that tag idea a bit, actually. It’s been tough so far, because the <img> tag in Flash seems to force a line break after itself. Not sure this is a usable approach, after all … but if I do figure something out, I’ll be sure to blog about it!

Leave a Reply