Tuesday, March 1, 2011

AS2 HtmlText Href listener

So a colleague of mine was working on an RSS reader that linked the title of the feed to the feed url.  Problem was he was lumping all of the text into one htmltext field. This was fine until we realized we havefor something a colleague was working on we needed to be able to run a tracking script when a user clicked on an htmltext href link. After a fair amount of Googling, I found the answer here.  As this was a truly glorious find, I named myself "Queen of the Mountain," of course. Anywho, the code

function myFunc(arg:String):void {

trace ("You clicked me!Argument was "+arg);
}
myTextField.htmlText ='<a href="asfunction:myFunc,Foo">Click Me!</a>';
 
By inserting the asfunction into the href and listing the function, you can call the function from within the html text. I thought that was sort of the coolest thing ever.  You may ask why not just use AS3's event listener (TextField.LINK) as joshiverson also describes.  Well this is for a banner, which many Flash banner ad developers may know still requires us to use AS2 to meet publisher specs, which is a debate for another time. :/ For now I have stretching my google and problem solving skills!