ColdFusion in Context: Logout Persuasion

Suppose you want the user to actually log out so you'll know it's OK to release variables in memory prior to their scheduled timeout. Users tend to simply close the window when they're done, and you won't know when they've done that.

One way to handle this problem is to create a parent window whose function is to open a child window for working and be available to nag the user into logging out if the user closes the work window directly.

Create the Context

Here is where the browser will go after logout; call it homepage.cfm.

Welcome to the login page.
<form>
<input type="button" name="doit" 
value="Press to simulate log in"?
onClick="javascript:window.location='nag.cfm';">
</form>

Here is where you would add code to remove the session from memory; call it bye.cfm.

You've passed through a page (bye.cfm)<br>
that would remove your session variables from memory.<br>
<script language="javascript">
window.location = 'homepage.cfm';
</script>

Here is where the user will "work"; call it work.cfm. It gives the user a button that will shift the focus to the nag page that invites the user to log out.

Here's your work page.  When you're tired of admiring it,<br>
click "Done" (or find nag.cfm).
<form>
<input type="button" name="leave" value="Done"
onClick="opener.window.focus();">
</form>

Do the Work

Pretending that login was successful and that the user has selected a work area, this code needs to open the work area, prime this page so it will check the focus the NEXT time the user touches it, and (if the user asks to log out) close the work area, call a page that would remove the session from the server if you were doing this for real, and deposit the user at the home page. Put all of the following code in nag.cfm.

First, prepare to open the work area. openchild1 names a new window "myChild1" when it creates it so we can use the name later to close it. Note that the features parameter that contains attributes such as toolbar and location (URL window) cannot have any spaces within it.

<head>
<script language="javascript">
function openchild1(mypage) {
  myChild1 = window.open('work.cfm','myWindow',
'toolbar=yes,location=yes,scrollbars=yes,resizable=yes');
}

Next, prepare to close the window when asked.

function leave() {
  if (!myChild1.closed) {
    myChild1.close();
  }
  window.location = 'bye.cfm';
}

Finally, close the script tag and set the body to open the work window when the page has finished loading and to try to close it when the user logs out.

</script>
</head>
<body onLoad="openchild1('work.cfm');">
<form>
<input type="button" name="doit" 
value="~~~ L O G  O U T ~~~" onClick="javascript:leave()">
</body>

Try It Out

Browse homepage.cfm and press the button. The work page should appear. If you click "Done", nag.cfm will come forward, asking the user to log out. If you simply close work.cfm, nag.cfm will still be there as a reminder. If you go directly to nag.cfm and log out, it will close work.cfm if it isn't already closed. You wouldn't normally notice bye.cfm, but in this demonstration, it has visible text as a reminder that it's there.

Expand the concept and make it nicer, but be sure you can use your method for major versions of both major browsers. [grim grin] Then, please tell us what you've learned so we can build on higher ground. =Marty=