Executing a CF page when the browser is closed or navigates away from a CF page.


I need to excute a CF page or file when the user closes the browser or navigates away from a specific CF page.

For example, if the users are closing the browser when they are on finalPage.cfm and I need to excute verifyDetails.cfm page. I also need to get an attribute from finalPage.cfm to verifyDetails.cfm to check my transaction or run verifyDetails.cfm page.

Are there any CF fucntions to do this and is this possible? If so, please let me know how I can do this?

Thanks on Advance.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Nothing can be done when the browser is closed or someone navigates away from your page.

However, you can use onSessionEnd function in your application.cfc file to run some CF code when the session expires.  The session will expire X minutes (usually 20) after the last activity by the user.

Philippe DamervalSenior Analyst ProgrammerCommented:
My first sentiment is to say no, that is the nature of the web, you don't control the client and your templates don't know when the browser was closed at the other end so you can't react to that. However, using Javascript, if a certain data condition arises you can react to it and pop up a dialog which will prevent the user from closing their browser until the dialog window has been closed.
Why don't you run whatever code you need to run on load final page? It's a matter of architecture. If you need to run code after the user is done with something, run the code before the final page, for instance when they press submit.


Tpaul_10Author Commented:
How about something like this.

<script language="JavaScript" event="onbeforeunload()" for="window">
<cfinclude template="act_Cancel.cfm">

This is working all the time(i mean whenever I move away from this page), but I need to get it worked
only when the window closed.

I need to find out how to use window.close function.

Any suggestions?
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Philippe DamervalSenior Analyst ProgrammerCommented:
Here's a link to a page where they seem to do this. It should apply t you despite the forum being .Net - let me know.

On before load does execute on every page, it's not applicable here.

It seems that forum is being driven by the click of a form button or link, not clicking the browser close button.

I have seen this question again and again on EE, never seen an answer.  I think you need to rethink it.  If you have clean-up to do, consider onSessionEnd()

Tpaul_10Author Commented:
Thanks for the reply guys and I am looking in to all these options

gdemaria: Can you please give me an example how to use onSessionEnd()?



Are you using applicaton.cfc or application.cfm ?  You need to use application.cfc if you're not, you would have to switch.

The function inside application.cfc called onSessionEnd simply fires whenever the session expires.  It will run whatever code you have in there.


What is it you want to do when the session expires?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tpaul_10Author Commented:
Unfortunately, I am still on CF 7 as well as not using CFC at all since it is a old website which was developed USING CF 5.0.


 Application.cfc is supported by Coldfusion 7

 What is it you want to do when the session expires?
Gurpreet Singh RandhawaCEOCommented:
Well! I am asking same scenario from @gd when i struck with a problem like yours using the OnBeforeUnload Event, but i suggest you this is not a valid solution!

You must use onSessionEnd as this method has many advantagaes and javascript is not the only solution
Tpaul_10Author Commented:
Thanks for the suggestion experts and i have changed the architecture to work differently.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.