AS3: TextEvent.LINK and ContextMenu incompatibilities.

July 23rd, 2007 by Luis

This morning I run into a small problem when using the new TextEvent.LINK.

Basically as with ActionScript 1 and ActionScript 2, ActionScript 3 allows you to associate ActionScript events with links in a textfield.

In AS3, the LINK event is dispatched when a user clicks a hyperlink in an HTML-enabled text field, where the URL begins with "event:". The remainder of the URL after "event:" will be placed in the text property of the LINK event.

Everything is good until here, but the problem comes when you right-click the link in the textfield. By default the flash player shows one type of context menu that is not any of the 3 types defined in the docs.

contextmenu

According with the AS3 language reference the Flash Player has three types of context menus: the standard menu (which appears when you right-click in Flash Player), the edit menu (which appears when you right-click a selectable or editable text field), and an error menu (which appears when a SWF file has failed to load into Flash Player).

The problem with the context menu appearing when you right-click a link inside a textfield is basically that igonres completely the new LINK event and by default tries to open the link generating an obvious alert message from the browser:

'Firefox doesn't know how to open this address, becuase the protocol (event) isn't associated with any program'.

So far I don't think there is a way to edit or modify this type of context menu.

Sample swf:

Sample Code:

Actionscript:
  1. package
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.events.TextEvent;
  5.     import flash.text.TextField;
  6.     import flash.text.TextFieldAutoSize;
  7.     import flash.text.TextFormat;
  8.    
  9.     [SWF(width="450", height="150", frameRate="31", backgroundColor="#FFFFFF")]
  10.    
  11.     public class TextEventLinkTest extends Sprite
  12.     {
  13.         private var myTextField     : TextField;
  14.        
  15.         public function TextEventLinkTest():void
  16.         {
  17.            
  18.             myTextField             =   new TextField();
  19.             myTextField.autoSize    =  TextFieldAutoSize.LEFT;
  20.             myTextField.multiline   = true;
  21.  
  22.             var format:TextFormat   =     new TextFormat();
  23.             format.font             =   "Verdana";
  24.             format.color          =  0xFF0000;
  25.             format.size             =   11;
  26.  
  27.             myTextField.defaultTextFormat = format;
  28.             myTextField.x=20;
  29.             myTextField.y=20;
  30.        
  31.             addChild(myTextField);
  32.        
  33.             myTextField.htmlText = 'With the new method of event handling in AS3,<br>' +
  34.                     'event (asfunction) no longer calls a method you defined but,<br>' +
  35.                     'rather, sends out a TextEvent (<b><u><a href="event:flash.events.TextEvent">flash.events.TextEvent</a></u></b>) of the<br>' +
  36.                     'type <b><u><a href="event: TextEvent.LINK">TextEvent.LINK</a></u></b> for the text field instance in which it exists.<br>' +
  37.                     '<br>The text following the event: keyword in the link is accessible from<br>' +
  38.                     'the TextEvent object through its text property.';
  39.            
  40.             myTextField.addEventListener(TextEvent.LINK, linkEvent);
  41.  
  42.         }
  43.        
  44.         private function linkEvent(event_:TextEvent):void
  45.         {
  46.             trace('click: '+event_.text);
  47.         }
  48.        
  49.        
  50.     }
  51. }


4 Responses to “AS3: TextEvent.LINK and ContextMenu incompatibilities.”

  1. Ashim Says:

    That is, however, pretty much how - javascript:funtionCall - works.
    Usefulness of the TextEvent.LINK is fantastic.

  2. Roy Tang Says:

    Hi, I’m facing this problem as well. I notice the post is about half a year old. Is it too much to hope you’ve found a solution to this problem? :p

    Thanks!

  3. Prasanta Saha Says:

    It does not work for AIR Applications. Is there any workaround?

  4. daniel Says:

    @Prasanta Saha: it is working pretty the same way with the AIR. seriously.

Leave a Reply