As you probably already know, Macromedia Flash has an awkward limitation for handling URLs longer than 127 characters as they get truncated.
Lately, while developing an eLearning project it was necessary to load rich text as xml. The solution that Macromedia propose to overcome this 127 length restriction, is static and didn’t help me much in this situation, because they propose to have one function to fire getUrl, for each instance of long url and because I load xml to render dynamically the rich text content, I couldn’t use that solution and I had to find a dynamic approach to solve my problem…
Here is a generic solution, I propose, for handling these long URLs.
First of all, we declare the UrlsManager class that will do the mapping of the real long url values, with short ids… The long value is replaced by the following piece of code: asfunction:anchorClicked(id) where anchorClicked is a method that will fire the appropriate method from UrlsManager class.
The definition of our class is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class md.utils.UrlsManager { static var urls:Array = new Array(); /** * this method is used to handle long urls **/ public static function addUrl(url:String):Number { urls.push(url); return (urls.length - 1); } /** * this method is used to open an url **/ public static function openUrl(id_url:String):Void { var id:Number = Number(id_url); getUrl(urls[id]); } } |
As you see our members are defined as static so basicaly this class will have a "global" behaviour assigning a unique id for each long url.
Our advantages of using such a static class, is that we don’t need to instantiate it and it can be referred from any point in the project like this: UrlsManager.openUrl(id_url) or UrlsManager.addUrl(url).
Having this class declared the only remaining bit we still need to do, to solve our problem is actually using it. So, the next piece of code explains how we use it to replace the long url value with a shorter one and how it is invoked.
function anchorClicked(id_url):Void { UrlsManager.openUrl(id_url); } if(paragraphs[p].childNodes[k].nodeName.toLowerCase() == "a") { paragraphs[p].childNodes[k].attributes.href = "asfunction:anchorClicked," + String(UrlsManager.addUrl(paragraphs[p].childNodes[k].attributes.href)); }
The solution is very simple, we replace the value of href attribute with this asfunction:anchorClicked(id_url) where id_url is a value returned by addUrl method. Invoking UrlsManager.openUrl(id_url) we actually invoke getUrl function of the original long value of the url.
I really hope you find this helpful! Cheers!