How to perform a cflocation when a session times out using a cfdiv

If you are using the cfform tag inside a cfdiv to submit the form without refreshing the page, then the short answer is you can't do a cflocation outside the cfdiv.

This problem cropped up on a recent project. The OnRequestStart function in application.cfc redirects to the login screen when a session times out. This works fine when submitting a form in the main page, but not when the cfform is inside a cfdiv and uses AJAX to refresh the contents of the cfdiv.

What does happen? The login screen appears inside the cfdiv. What I want to happen is to redirect the main page (which contains the cfdiv) to the login screen. How cfform functions inside the cfdiv tag is covered in a blog post by Ray Camden.

To sum up Ray's post, a cfform inside the cfdiv tag uses ajax to submit the form and return the results inside the cfdiv without reloading the container page. This feature is great and I'm using it extensively, but presents a challenge when using the OnRequestStart function to redirect.

The solution is to use javascript for your redirect instead of cflocation. You can download the example code. Below are the main points.

See a live example

Download the code

First you need to modify your application.cfc file.

application.cfc

<cffunction name="onRequestStart" returnType="boolean" output="false">
  <cfargument name="thePage" type="string" required="true">

  <cfset request.LogoutNow="false">
  <cfif session.login eq 0>
    <cfset request.LogoutNow = "true">

    <cfset CurrentPage=#gettoken(cgi.CF_TEMPLATE_PATH,listlen(cgi.CF_TEMPLATE_PATH,"/"),"/")#>
    <cfif listfindnocase("index.cfm", CurrentPage)>
      <cflocation url="login.cfm" addtoken="no">
    </cfif>
  </cfif>
  <cfreturn true>
</cffunction>

 

Next, you need to add a hidden form field called LogoutNow to your cfdiv page. You'll also add the AjaxOnLoad function. What does the AjaxOnLoad function do? It's very similar to the onLoad method which you put in the body tag. It fires the javascript function name passed as it's parameter. It only fires after the cfdiv contents are done loading.

mycfdiv.cfm

  <cfparam name="LogoutNow" default="false">
<cfif isDefined('form.SubmitButton')>
  <cfset LogoutNow = "#request.LogoutNow#">
</cfif> <cfform method="post" name="myForm" action="#cgi.SCRIPT_NAME#">
<p>
This form is a CFFORM inside a CFDIV tag.
</p>
<p>
It will submit asynchronously using AJAX.
</p>
<cfinput type="hidden" name="LogoutNow" id="LogoutNow" value="#LogoutNow#"/>
<cfinput type="submit" name="SubmitButton" value="Submit Form">
</cfform>

 

<cfset AjaxOnLoad("InitDiv")>

In the main page (index.cfm), my javascript function InitDiv is called when the cfdiv is done loading. The CheckLogout function is called and if LogoutNow is true, then reload the main page. This will fire the OnRequestStart function in application.cfc and redirect us to the login screen since our session is expired.

index.cfm

<script>
InitDiv = function() {
   CheckLogout();
}

CheckLogout = function() {
   if(document.getElementById('LogoutNow').value =='true') {
     window.location.href = 'index.cfm';
   }
}
</script>

 

Feedback, thoughts or suggestions always welcome.

 

0 responses to “How to perform a cflocation when a session times out using a cfdiv”

Leave a Reply

Leave this field empty:

Powered by Mango Blog.