8.28. Adding a Hyperlink to Text

Problem

You want to hyperlink some of the text displayed in a text field.

Solution

Use HTML <a href> tags within the object’s htmlText property. Alternatively, use a TextFormat object with a value assigned to the url property.

Discussion

Both solutions to this problem require you to set the text field’s html property to true:

myTextField.html = true;

If you want to use HTML to add a hyperlink, add an <a href> tag to the text field’s htmlText property, as follows:

myTextField.htmlText = "<a href=\"http://www.person13.com/\">click here</a>";

You can specify a target window in which to open the link by adding a target attribute to the <a href> HTML tag. For example:

myTextField.htmlText = "<a href=\"http://www.person13.com/\" target=\"_blank\">click 
here</a>";

When text is hyperlinked in Flash, the mouse cursor changes to a hand when it is over the linked text. Flash does not inherently provide any indication that the text is linked, as do most HTML browsers (with an underline and color change). For this reason, it is helpful to add HTML markup that underlines and colors the linked text:

htmlLink = "<font color=\"#0000FF\"><u>";  // Add the color and underline.
htmlLink += "<a href=\"http://www.person13.com/\">click here</a>";
htmlLink += "</u></font>";
myTextField.htmlText = htmlLink;           // Close the color and underline.

You can accomplish the same tasks without HTML by using a TextFormat object. The TextFormat class includes a url property for just this purpose. ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.