DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 8.28 Adding a Hyperlink to Text

8.28.1 Problem

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

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

8.28.3 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. Assigning the URL to the url property will link the formatted text. For example:

myTextField.text = "click here";
myTextFormat = new TextFormat(  );
myTextFormat.url = "http://www.person13.com/";
myTextField.setTextFormat(myTextFormat);

To specify a target window in which to open the link, set the value of the TextFormat object's target property, as follows:

myTextField.text = "click here";
myTextFormat = new TextFormat(  );
myTextFormat.url = "http://www.person13.com/";
myTextFormat.target = "_blank";
myTextField.setTextFormat(myTextFormat);

As with the HTML technique, when using a TextFormat object to create a hyperlink, Flash does not offer any indication as to the link's presence other than the hand cursor when it is moused over. You can add color and/or an underline to the linked text to let the user know that it is a link. You should use the TextFormat object's color and underline properties for this purpose:

myTextField.text = "click here";
myTextFormat = new TextFormat(  );
myTextFormat.color = 0x0000FF;
myTextFormat.underline = true;
myTextFormat.url = "http://www.person13.com/";
myTextField.setTextFormat(myTextFormat);

You can use either of the techniques in this recipe to add links that point not only to http and https protocols, as shown in the examples, but also links to other protocols. For example, you can use the same techniques to open a new email message:

myTextField.text = "email me";
myTextFormat = new TextFormat(  );
myTextFormat.color = 0x0000FF;
myTextFormat.underline = true;
myTextFormat.url = "mailto:joey@person13.com";
myTextField.setTextFormat(myTextFormat);

Be aware, however, that many other types of links (such as mailto links) will work only when the movie is being played in a web browser in which a default client for the protocol has been defined.

8.28.4 See Also

Recipe 8.10 and Recipe 8.18. Recipe 9.6 shows how to test whether a string has the form of a valid email address. Recipe 18.4 explains how to send data to a server-side script, which could then send the email for you. Recipe 20.10 explains how to call ColdFusion functions from Flash. You can use Cold Fusion's <cfmail> tag to send email from a ColdFusion server.

    [ Team LiB ] Previous Section Next Section