Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirecting from <cfscript> 1

Status
Not open for further replies.

spook007

Programmer
Joined
May 22, 2002
Messages
259
Location
US
I would like to redirect the user once a certain criteria has been met. I know that you can do this with the <cfhttp> tag, but how do you do it when you've got it in between <cfscript> tags?

<cfscript>
if (session.textonly){
redirect to http:/blah... blah...
}else{
redirect to http:/blah... blah...
}
</cfscript>

Is there some command you can use to redirect when you have code in the above format? Thanks!
 
2 things.

First, you probably already know that CFSCRIPT only directly supports a subset of CFML functionality. It lacks a CFHTTP equivilent.

But second... CFHTTP won't intrinsically redirect the user. You can call CFHTTP to retrieve another page and display it within the current page... but in order to actually redirect the user, you'd probably want to use CFLOCATION.

You could certainly do something like:
Code:
<CFSCRIPT>
   if (session.textonly){
     redirectURL = &quot;[URL unfurl="true"]http://blahblah1...&quot;;[/URL]
   }else{
     redirectURL = &quot;[URL unfurl="true"]http://blahblah2...&quot;;[/URL]
   }
</CFSCRIPT>

<CFLOCATION url=&quot;#redirectURL#&quot;>

If your script block is huge, you can add breaks to get you out:
Code:
<CFSCRIPT>
   redirectURL = &quot;&quot;;
        :
        :
   if (session.textonly){
        :
     redirectURL = &quot;[URL unfurl="true"]http://blahblah1...&quot;;[/URL]
     break;
   }elseif ... {
        :
        :
     redirectURL = &quot;[URL unfurl="true"]http://blahblah2...&quot;;[/URL]
     break;
   }
</CFSCRIPT>

<CFIF Len(Trim(redirectURL)) GT 0>
   <CFLOCATION url=&quot;#redirectURL#&quot;>
</CFIF>

<p>No redirection took place!</p>


Or, if you're using MX and above, you can set up a virtual User Defined Function that CFSCRIPT can call (this is untested... I'm pretty sure it should work):
Code:
<CFFUNCTION name=&quot;redirectUser&quot;>
 <CFARGUMENT name=&quot;inURL&quot; type=&quot;string&quot; required=&quot;yes&quot;>
 <CFLOCATION url=&quot;#inURL#&quot;>
 <CFRETURN true>
</CFFUNCTION>

<CFSCRIPT>
   if (session.textonly){
     redirectUser(&quot;[URL unfurl="true"]http://blahblah1...&quot;);[/URL]
   }else{
     redirectUser(&quot;[URL unfurl="true"]http://blahblah2...&quot;);[/URL]
   }
</CFSCRIPT>

or something like that.


-Carl
 
Carl;

I appreciate your advise, I have tried to use your example with CFLOCATION, I am experiencing problems when redirecting though. I'm assigning a session variable in the same code before I redirect, when the browser redirect and opens the new page it continues to displays the old session variable. When I manually refresh the new page, the new session variable is displayed. When I redirect is it pulling the page from the cache? How can I force it to load the page everytime? Thanks
 
Kay... found what it was doing. My page was caching everytime and was not loading the new info. I just added info to my header to force a refresh everytime. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top