ColdFusion in Context: Unique Links

You've read articles showing that under some circumstances, users will see an old copy of Web pages instead of the real thing. Worse, links from your site to other applications in your organization are starting to return old data, and you can't just wade in there and set headers on those target pages to fix the problem.

You know you can force the browser to bring back a new page by making the URL unique. So, you want to jump to the most foolproof method around: changing the URL so that browsers and caches won't find a match and will have to cough up fresh data.

However, it's not enough to create a unique URL on the server. As you know, making the link unique by using code on the Web server has a basic limitation: the back button. No matter what you do on the server, if the browser doesn't get a fresh copy of the page that has the link, you won't get a new link.

Almost

To illustrate this, create almost.cfm and give it this code. It uses a ColdFusion function to add a random number to the link, making the URL almost unique every time a user gets to the page without pressing the back button to do it:

<cfoutput>
<a href="newpage.cfm?Nr=#rand()#">
Server Link</a><br>
<a href="newpage.cfm/Nr/#rand()#">
Server Extra Path</a>
</cfoutput>

And then create newpage.cfm and have it display a unique identifier using ColdFusion's createUUID function to show that you aren't getting the same page over and over:

<cfset Unique=createUUID()>
<cfoutput>
#Unique#
</cfoutput>

Browse almost.cfm and click the link to reach newpage.cfm. Look carefully at the URL. Now press the back button and do it again. When you do it again, you'll see that the link doesn't change! If circumstances were bad, you would see the identifier repeat on newpage.cfm as a result. (Fortunately, most of the time, circumstances aren't that bad.)

Go back. This time, refresh almost.cfm, and the link will change. This approach to unique links is OK if the user can't go back. Otherwise, a fundamentally different approach is needed.

Unique

What you need is a method of changing the link on the client side rather than on the server side so that it's always different. Javascript can help.

Create and browse unique.cfm. Notice that the link doesn't have a true location any more. Instead, the desired location is a parameter to a function that appends the milliseconds since a fixed date in the past and then tells the browser to go to the resulting URL.

<head>
<script language="javascript">
function goto(Link) {
now=new Date();
var Nr=now.getTime();
Link=Link+Nr;
document.location=Link;
return true;
}
</script>
</head>

<a href="javascript:goto('newpage.cfm?Nr=')">
Javascript Query String</a><br>
<a href="javascript:goto('newpage.cfm/Nr/')">
Javascript Extra Path</a>

Application

Some users actually have browsers set to "Never" look for a fresh page or to look for a newer page only once per session. Perhaps you have links to other applications in your organization, and you therefore can't just set the headers on the target page to overcome this problem. If that's what's troubling you, this technique can at least get your users started down the right road. =Marty=