A little while ago, we saw how to use the property htmlText of the TextField class to add a link to a text field with Actionscript 3.0.
That time, we did not use any event to tell Flash to redirect the user to a specific page.* The event was included in the HTML tag <a> using the attribute 'href'.
*
So, I ask myself, if I have a server script which returns the html text with the tag <a> but instead of using it to redirect the user to a web page,* I would like to reuse it in my script for other means, how would I proceed'
In this tutorial, we will see how to use an HTML string with a tag <a> assigned at the property 'htmlText' of a text field but without, once clicked, the redirection to another web page.
Using, the event LINK of the TextEvent class, we can tell the function called by this event to start a given code still retrieving the url from the HTML tag <a>.
Let us see how to do it' I create a FLA and save it as 'main.fla'.
I create a Document Class, an AS file saved as 'Main.as', implemented the following way:
Code:
package
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.TextEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
public class Main extends MovieClip
{
private var field:TextField;
public function Main()
{
createField();
}
private function createField():void
{
field=new TextField();
addChild(field);
field.selectable=false;
field.wordWrap=true;
field.textColor=0xFFFFFF;
field.x=50;
field.y=50;
field.width=150;
field.htmlText="Clicca quì per vedere il mio sito web";
field.addEventListener(TextEvent.LINK,clickin);
}
private function clickin(evt:TextEvent):void
{
trace(evt.text);
}
}
}
Let us analyse the code
Properties
An instance if the TextField class
private var field:TextField;
Methods
createField();
I create a text field with the needed properties of the moment
field=new TextField();
addChild(field);
field.selectable=false;
field.wordWrap=true;
field.textColor=0xFFFFFF;
field.x=50;
field.y=50;
field.width=150;
Here is the principal point of the tutorial. The tag <a> is preceded of 'event'. This allows to tell Flash not to call directly the browser once the text clicked.
I add a listener to the event LINK of the TextEvent class which will call the method 'clickin'
field.htmlText="Clicca quì per vedere il mio sito web";
field.addEventListener(TextEvent.LINK,clickin);
clickin();
I now retrieve the url from the HTML string and we can now reuse it as wanted.
trace(evt.text);
The trace output will be as followed:
Stay tuned !
Bookmarks