Birnam Designs is a quality web design and development agency in Virginia

asfunction — calling actionscript from a flash HTML textbox

asfunction: is one of those neat little tricks that I almost never use, so I usually end up having to look up the exact terminology every time. If you already know about asfunction:, maybe you’re like me and are looking it up again for the zillionth time, but now maybe you’ll be able to remember where to find it. If you don’t know about asfunction:, it can really come in handy sometimes, so check it out:

asfunction: is the Actionscript equivalent to calling javascript: from a link in a standard HTML page.

In HTML/javascript, the syntax looks like this:

<a href="javascript:function(a)">link text</a>

Where everything after “javascript:” is evaluated directly by the javascript engine, and is run when the link is clicked. asfunction: looks very similar, and is defined within the HTML in a flash textfield:

<a href="asfunction:function,a">link text</a>

There are certainly some differences, but I think it’s a good thing that Macromedia chose to use familiar syntax.

The most obvious difference is that the function parenthesis are dropped and the the parameter is simply delimited by a comma from the function name. Why not just evaluate a normal function call, like javascript: does? Well, unlike javascript, which is a purely interpreted language, actionscript is actually processed into a more rudimentary language (known as p-code) when the swf is compiled. I don’t have any actual information to back this up, but my hunch is that the [function name],[parameter] syntax more closely follows the format of an actual p-code function call — so in reality, it IS more or less an actual evaluation of the asfunction: text.

While the syntax appears to be the big difference, there are a few more fundamental points that this example doesn’t show very well.

  1. Even though I’m comparing it to javascript:, it’s called asfunction: and not actionscript: for a reason — only a function can be called with asfunction:. While javascript:var f=5; alert(f); is valid with javascript: evaluation, it is not possible with asfunction:.
  2. Only strings can be passed with the asfunction: parameter. Booleans, numbers, etc. are all read as literal strings.
  3. Only a single parameter can be passed. If you try to pass more parameters, the commas just get sucked in the string of the first parameter. So asfunction:getURL,http://www.birnamdesigns.com,_blank will simply call getURL with “http://www.birnamdesigns.com,_blank” as the single parameter. Of course, you can always get around this by using your own functions will split the first parameter by commas.

In addition to these big differences, here are a few extra tips and guidelines:

  • Spaces count — so no spaces after the colon or after the comma. This can be particularly confusing because the Help documentation in Flash shows a space after the comma in its example. I can’t get that to work, though.
  • Object hierarchies can be used. So if you have “showText(s)” defined locally to the textfield, and another “showText(s)” defined on the movie’s _root, asfunction:showText,hi and asfunction:_root.showText,hi will call different functions.
  • The function parameter is entirely optional.

This should be able to get you started — and hopefully by writing this I’ll be able to remember all of this next time I’ll need to look it up.

Comments are closed.