SWFAddress url sorting utility
UPDATE:
I don’t know how I missed this, but since v 2.1 SwfAddress has the getPathNames() method which does exactly what my bit of code does… ah well. Anyway I’ll leave it up as it has the basics of string splitting and other stuff…
Thanks to Cam for pointing this out… and while you’re at it check out his blog, he’s a much better coder than myself (but sucks at the guitar
)
–
Just thought I’d post this up as it might be helpful to someone…
Normally if you’re building a site using SwfAddress (and you should, not having any form of deeplinking in your flash site these days is utterly unforgivable) you could just on the swfAddress change do something like
switch(String(e.value).replace("/", "")){
case "news" :
openNews();
break;
case "shows" :
openShows();
break;
}
But what happens if you want to use subpages (/news/news-item), or subsubpages (/news/news-item/photo)… or however many… well, something like this will help you a lot.
var string:String = "/news/news-item";
function sortDeepLink(_deeplink:String):void{
var urlArray:Array = [];
if(_deeplink.charAt(_deeplink.length - 1) == “/”){
urlArray = _deeplink.substring(1, _deeplink.length - 1).split(”/”);
} else {
urlArray = _deeplink.substring(1, _deeplink.length).split(”/”);
}
switch(urlArray.length){
case 0 :
//Home address
trace(”Home”);
break;
case 1 :
//One tier address - ie. /news/
trace(”URL (one tier): ” + urlArray[0]);
break;
case 2 :
//Two tier array - ie. /news/news-item
trace(”URL (two tier): ” + urlArray[0],urlArray[1]);
break;
default :
//Invalid URL
trace(”URL invalid”);
break;
}
}
sortDeepLink(string);
//
You can extend the switch function to handle as many tiers as you need by just adding more cases - you can also add a 404 function in the default case if you want.
I’d love to know if anyone uses this, so post a comment if you do!
