Flex: Clickable Text Links

Here's an example of creating clickable links within your Flex Text controls. A couple of things to note:

  1. You cannot set the selectable attribute to "false". Only "true", the default, will work. The Text control extends Label, which has this odd condition placed on it.
  2. I've found that when there are multiple links within one Text control, you sometimes need to click twice for the link click to be broadcast. See www.torontoflex.com for an example.
  3. Note that you need a link event handler on the Text control.
  4. The href attribute in your anchor tag must be prefixed with "event:"
  5. Right-clicking the link gives you options on how to open the link, but this functionality is broken (at least in FireFox) because Flash player does not strip the "event:" prefix before sending the URL to the browser (geez!)

Here's a fully working sample:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application width="100" height="100" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" styleName="plain">

   <mx:Script>
      <![CDATA[
         import flash.net.navigateToURL;
         
         private function onLinkClick( event:TextEvent ):void {
            var url:URLRequest = new URLRequest( event.text );
            navigateToURL( url, '_blank' );
         }
         
      ]]>
   </mx:Script>

   <mx:Text id="myText" link="onLinkClick( event )" selectable="true" fontSize="14" condenseWhite="true">
      <mx:htmlText>
         <![CDATA[
            <a href='event:http://www.adobe.com/'><u><font color='Blue'>Try this link!</font></u></a>
         ]]>
      </mx:htmlText>
   </mx:Text>
   
</mx:Application>

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
ikezi kamanu's Gravatar Note:

You need not dispatch/listen to a link event (TextEvent.LINK) to launch URL links. a href='http://www.adobe.com/' is a simpler implementation.

However, the link event is handy for triggering actions in the flex application though, like in-app navigation, printing, etc.

-ekz
# Posted By ikezi kamanu | 8/27/07 9:55 PM
Oliver Merk's Gravatar If you do not listen for the event, clicking on the link does nothing (Flex 2.01). You can, however, right-click and open the link in a new window, etc. Maybe this is fixed in Flex 3?
# Posted By Oliver Merk | 8/28/07 9:29 AM
samuel's Gravatar can i use a variable in the "event: ..." to define in actionscript where the link goes?
# Posted By samuel | 3/17/09 7:14 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.