The right way to adjust URLs in ColdFusion

(This is primarily a note-to-future-self, but I figure others might have a use for it.)

I’m working on some code that fetches favicon files. Of course, the tricky thing about favicon files is that they can be in several different places. If I want to create an array of the different places to check, given a starting point, what’s the right way to do that in ColdFusion?

Your first guess might be something involving list or array functions. Let me provide a few counterexample starting points:

It turns out that it’s even easier than that: (in cfscript for clarity)

var link = createObject("java", "").init(;
var relativeLink = createObject("java", "").init(link, "./favicon.ico").toString();
var absoluteLink = createObject("java", "").init(link, "/favicon.ico").toString();

As you would hope, this yields the following results for that second example:

link         =
relativeLink =
absoluteLink =

You can see from the code we’re just using two different forms of the URL constructor. The first form, with just the string, creates a URL object from your base link. The second form, which now includes that first URL object and a relative path, does the interesting work of cracking open the URL and fixing it to what you expect it to be.