(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:
http://example.com
http://example.com/quux/index.php?foo/bar
It turns out that it’s even easier than that: (in cfscript for clarity)
<cfscript> var link = createObject("java", "java.net.URL").init(arguments.link); var relativeLink = createObject("java", "java.net.URL").init(link, "./favicon.ico").toString(); var absoluteLink = createObject("java", "java.net.URL").init(link, "/favicon.ico").toString(); </cfscript>
As you would hope, this yields the following results for that second example:
link = http://example.com/quux/index.php?foo/bar relativeLink = http://example.com/quux/favicon.ico absoluteLink = http://example.com/favicon.ico
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.