Tuesday, May 31, 2011

ADF: Tip-1, Achieving onunload event with ADF Faces


As i frequently work on Oracle ADF, i've decided to write tips on this Framework.
Previous version of adf (10g) had a <afh:body> component to represent the body of your page which allowed to specify javascript events like onload or onunload.
In ADF RC, the body (as well as html and head) is geneated by a single<af:document>.
To implement the onXXXXX events you embed an <af:clientListener> within this <af:document> and specify property type="load" with the method to trigger.
Problem is that onunload property is not supported by th af:clientListener. So how do i do ?
Well, a solution is to add this event during page load.
First step :
     Embed in your <af:document> the following :

1
<af:clientListener method="addOnUnload()"/>
Second step :
    Add your unload event using this kind of script :

1
2
3
4
5
6
7
8
9
10
<af:document>
  <f:facet>
    <f:verbatim>
     <script type="text/javascript">
         function addOnUnload(evt) {
            window.onunload=function()
              { alert('Goodbye Word'); }
        }
</script>
</f:verbatim>
When closing browser your event will be triggered as expected.

1 comment :

  1. The alert would fire fine . But lets say I have a usecase where on closing the browser , I need to call a server Listener to execute some custom logic. That doesnot work at all -
    https://forums.oracle.com/forums/thread.jspa?threadID=2236455&tstart=0

    ReplyDelete