O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required