[thelist] FREE TIP

Joshua Olson joshua at alphashop.net
Mon Aug 13 17:24:20 CDT 2001


<tip type="ColdFusion" author="Joshua Olson">
One of the peskiest parts of web development is the cancel button.  Whenever
a user hits the cancel button, they expect to be taken back to wherever they
were before.  Furthermore, they expect to be able to hit cancel 3 times to
take them back to where they were 3 steps ago.

This becomes a pretty serious problem when you are using a resource that is
shared among a couple of different application, such as a global image
library, because you cannot easily hardcode where the cancel takes you.
Additionally, if they move around the shared resource, you have to pass the
original return path with you wherever you go.  If the URL is complex, the
whole system can get fairly maddening pretty quickly.

The easiest solution is to use a session variable to store the return path
for the cancel action and name the variable something unique to the
resource.  Make sense?  If not, I'm sorry--not enough time to explain it
here.

If you are still following, you'll notice a problem when someone has two
browsers open, or they bookmark the resource.  The place that you return to
could get mixed up and everything goes to hell and back!

So, the only viable answer is to store the return URL in the request URL,
but again, that becomes ugly.  So, instead, store a reference to a session
variable which contains to return URL.

Put this at the top of the resource that has the "cancel" button (I'll
explain the assumption in a bit)

<cfparam name="url.return_url" default="">
<cfparam name="url.return_id" default="">

<cfif NOT Len(app.return_id)>
  <cfset done = "0">
  <cfloop condition="NOT done">
    <cfset t = "return" & RandRange(1, 100000)>
    <cfif NOT StructKeyExists(session, t)>
      <cfset done = "1">
    </cfif>
  </cfloop>

  <cfset session[t] = url.return_url>
  <cfset url.return_id = Replace(t, "return", "")>
</cfif>

<cfset url.return_url = session["return" & url.return_id]>

Assumptions:

1. return_url contains the calling application's url with query string
2. all intra-resource url's contain the variable
resource_id=#url.resource_id#

All you have to do is make sure that all links within the resource have the
variable mentioned in number 2, and the url of the cancel button is <a
href="#url.return_url#">

If you want to use a subsequent level of cancel after this, simply construct
a URL which passes the current URL as the parameter "return_url" and omit
the "return_id".  When you cancel from that one, you'll be returned to the
current (and thus get return_id back), which will then cancel to the
original location!

Use this for a bit and you'll begin to see the benefit of using this
technique.

Dang, that was a mouthful.
</tip>

-joshua





More information about the thelist mailing list