Browser Redirection

At times, you may wish to redirect a user’s browser to a location other than the current template. This is generally handled using the cflocation tag. The following example creates a drop-down box listing several web sites. Choosing one of the sites and clicking the Go button posts the form to itself and uses the cflocation tag to redirect the user to the selected web site.

<cfif IsDefined(form.Go')>
  <cflocation URL="http://#form.Goto#" addtoken="No"> 
</cfif>
   
<h2>Please choose a location:</h2>
<cfoutput>
<form action="#CGI.Script_Name#" method="Post">
</cfoutput>
  <select name="Goto">
    <option value="www.macromedia.com" Selected>Macromedia</option>
    <option value="www.yahoo.com">Yahoo</option>
    <option value="www.amkor.com">Amkor</option>
  </select>
<input type="Submit" name="Go" value="Go">
</form>

The URL attribute specifies an absolute or relative path to the page you want to redirect the user’s browser to. addtoken is an optional attribute and indicates whether to append client variable information (CFID and CFToken values) to the end of the URL specified in the URL attribute. In order to use the addtoken attribute, clientmanagement must be turned on in the Application.cfm file. The default value for addtoken is Yes.

You can also redirect to a different page using the cfheader tag:

<cfheader name="Refresh" value="0; URL=http://www.example.com/mytemplate.cfm">

This code uses the cfheader tag to generate a custom HTTP header with a Refresh element that contains ...

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.