function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

how to execute the controller on window unload ??

For some reason, I have to catch the event when a user is leaving a visualforce page (closing the page, clicking on another link, ...) and execute a method of my controller.

The idea was to use something like window.onbeforeunload = function(){ Unload();return false} which should call the function "unload()". It's working pretty well when I replace the "Unload()" with a javascript alert, meaning the function is executed but the aim is to call the controller... So I have a simple actionfunction  <apex:actionFunction action="{!OnUnload}" name="Unload"/> which should call the method "OnUnload" in my controller. Unfortunately, it is not working... It's not the first time I'm using actionfunction called from Javascript but it looks like that the line "actionfunction" is already "unloaded" even on "onbeforeunload" :/

Has someone already been confronted to this problem ?? 


i am throwing my thoughts lets try

put a commandbutton in invisble state and in closing or clicking on other link call a javascript function. In that click the command button through ur code...some thing like this 

command button code:

<apex:commandButton id="cmdSearch" reRender="RefreshDiv" value="  Search  " style="display:none;" status="strMes" action="{!Search}" />

javascript code:


then put the 

window.close() method.





Soul AgesSoul Ages

I have a similar problem. Did you find a way of using actionFunction in onUnload?


From my experience using unload will not work.  You have to use onbeforeunload but this only seems to work in FF and IE not Google Chrome.  It may also not work in Safari but I'm not 100% sure of that.


Other thing I had to do get the the controller  method to work was to add a dummy value to the rerender attribute:


<apex:actionFunction name="cleanUp" action="{!cleanUp}" rerender="MyPage">

Darryl SinghDarryl Singh

Easy solution works in IE and Chrome.  Probably works in most other browsers, but I haven't tested them.  Enjoy!


<apex:page standardController="Any_Object" extensions="Any_Object_Ext" >
     <script type="text/javascript" >
          window.onbeforeunload = unloadPage;
          function unloadPage(){anyControllerMethod();}
     <apex:form >
          <apex:actionFunction name="anyControllerMethod" action="{!anyControllerMethod}" />



Jasmeet Singh 10Jasmeet Singh 10
I have been trying to do the same  Tried the onbeforeUnload event, but the challenge I am facing here is that I am not able call the action function through JS function as shown below code. Any help is appreciated.
<script language="JavaScript" type="text/javascript">
                window.onbeforeunload = unloadPage(); 
                 function unloadPage() {  

<apex:actionfunction id="SignOff" name="SignOff" action="{!SignOffLead}" oncomplete="alert(Sign Out Complete);" />

Only the 'Before' alert is coming.