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
shiva pendemshiva pendem 

How to detect window Close Event in Vf page

HI Guys,

We are using  partner communities . So if the User login  by entering the credentials and when user logouts i need to perform some action. this is fine. But when user closes the window directly i need to perform same action which performs while logout. Please let me know how to detect the window close event and perform the actions in apex class . 


please advise us.

Thanks
Shiva.
Mudasir WaniMudasir Wani
Hi Shiva,

It can be done Have a look on sample below script code.
It will run evertime a window is closed.
See if you can fire some remote action using salesforce Remoting.

Include below code in your page

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
jQuery(window).bind(
    "beforeunload", 
    function() { 
      var actionValue = confirm("Do you really want to close?");
        alert(actionValue);
        if(actionValue ==true){
            alert('close window');
        }else{
            alert('Do not close window');
        }
      return actionValue;
    }
)
</script>

//Java script remoting help
http://www.salesforce.com/docs/developer/pages/Content/pages_js_remoting.htm

Please mark this as solution if this solves your problem, So that if anyone has this issue this can help.