Categories Displayed in Flash

Flash: TextField actionscript hyperlink in AS3.0

In this article we cover how to get flash.text.TextFields HTML anchors/hyperlinks, to call actionscript functions, how to use CSS to style the up, over/hover and down/active and [UPDATE] visited states similar to hyperlinks in normal html.

The applications of this are numerous. Perhaps clicking a hyperlink will deeplink into your Flash application without navigating away, or perhaps it would open up a dictionary definition on the term.

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)
In this example clicking on the 3 links will show in the bottom text box where the link was meant to go.  This is based on the event handler receiving a flash.events.TextEvent.LINK event, as dispatched by formatting the '<a href=..' to dispatch the link event instead of the default navigateToURL.

The 'blog1' and 'blog2' links both go to the same URL, when either are clicked on both will change to the visited style.   This change color requires a hack, as the 'a:visited' isn't one of the CSS tags supported by Flash.   However we CAN overwrite the CSS style to display a different style, on the click.   This is more complex as we have to figure out, a) are we interested in changing all links that point to that location? or b) just the one that was clicked on?.   Keep in mind that this is a hack, and differs from the way browsers behave the links won't persist past that flash session, or across different TextFields unless you code it that way.

In a future article I'll discuss how to clean and process HTML, and replace the links with internal flash link's if you are using CMS'd, progressive enhancement websites, with Flash as the RIA style/presentation layer.  So the same HTML will work as expected when in HTML only site (e.g. for search bots) or in the Flash only.

  1.  
  2. import flash.text.StyleSheet;
  3. import flash.events.TextEvent;
  4.  
  5. var style:StyleSheet = new StyleSheet();
  6.  
  7. var hover:Object = new Object();
  8. hover.fontWeight = "bold";
  9. hover.color = "#00FF00";
  10. var link:Object = new Object();
  11. link.fontWeight = "bold";
  12. link.textDecoration= "underline";
  13. link.color = "#555555";
  14. var active:Object = new Object();
  15. active.fontWeight = "bold";
  16. active.color = "#FF0000";
  17.  
  18. var visited:Object = new Object();
  19. visited.fontWeight = "bold";
  20. visited.color = "#cc0099";
  21. visited.textDecoration= "underline";
  22.  
  23. style.setStyle("a:link", link);
  24. style.setStyle("a:hover", hover);
  25. style.setStyle("a:active", active);
  26. style.setStyle(".visited", visited); //note Flash doesn't support a:visited
  27.  
  28. html_txt.styleSheet = style;
  29. var htm:Array = new Array();
  30. htm.push("
  31. <p align="center">Example of HTML hyperlinks calling actionscript in AS3.0
  32.  
  33. ");
  34. htm.push("
  35.  
  36. Click to visit <a href="event:http://www.TroyWorks.com">home</a> ");
  37. htm.push("<a href="event:http://www.TroyWorks.com/blog/">blog1</a> ");
  38. htm.push("<a href="event:http://www.TroyWorks.com/blog/">blog2</a>");
  39. html_txt.htmlText= htm.join("");
  40.  
  41. function onHyperLinkEvent(evt:TextEvent):void {
  42.         trace("**click**"+ evt.text);
  43.         define_txt.text = ( "Clicked on " + evt.text);
  44.         var str:String = html_txt.htmlText;
  45.      //// update the link to the 'visited' state'
  46.  
  47.         str = str.split("'event:"+evt.text+"'").join("'event:"+evt.text+"' class='visited' ");
  48.         html_txt.htmlText = str;
  49. }
  50.  
  51. html_txt.addEventListener(TextEvent.LINK, onHyperLinkEvent);

2 responses to 'Flash: TextField actionscript hyperlink in AS3.0'

1. twfan said:
Made on 16.3.08 @ 11:53 pm

Great article, thanks! Is there a way to set a visited link in Flash?

2. twfan said:
Made on 17.3.08 @ 10:55 am

Thanks, Troy! This a big help. i also appreciate your lightning fast response. This is a great blog. Thanks for all your hard work.

Add a Comment:

You must be logged in to post a comment.