Author Topic: OnUnload  (Read 5429 times)

aevett

  • Customer
  • Sr. Member
  • *
  • Posts: 482
    • View Profile
OnUnload
« on: May 26, 2009, 02:42:54 PM »
For our web portal application, we think we need to use something like OnUnload to delete the JSessionId cookie.  This is because if the user leaves the web portal for another web page and then comes back, our rules for determining the context of the request get confused, and the request goes into ajaxswing rather than freemarker.  We found this link http://www.webdeveloper.com/forum/showthread.php?t=175314 which seems to indicate OnUnload can be used for this purpose, although the example javascript seems to be incomplete.

Should this be possible?  Would AjaxSwing need to add support for a custom OnUnload?  We actually would also like to remove the HTTP WWW authentication attached with the URL in addition to the cookie, but this seems less doable.

Support Team

  • Administrator
  • Hero Member
  • *****
  • Posts: 1074
    • View Profile
Re: OnUnload
« Reply #1 on: May 26, 2009, 02:48:56 PM »
You can easily provide your own onunload event handler already. The <body> tag is declared in the page template. You can do it in AjaxSwing/conf/templates/custom-page/page.html file. You can copy the default template directory to something like cleo-page so that it doesn't get overwritten with next version, and then specify your custom directory in the application properties file.

You can declare your custom onload function in custom.js to keep the template lean.

kdiamond

  • Customer
  • Jr. Member
  • *
  • Posts: 53
    • View Profile
Re: OnUnload
« Reply #2 on: May 29, 2009, 02:19:40 PM »
We are not having success with this yet.  Here are the steps we've taken.  We added the following 'onUnload' reference to page.html:

  <body onLoad="onLoad()" class="${page.cssClass}" onUnload="onUnload()">

And then the following to page-header.html:

   function onUnload() {
      ${page.onUnloadScriptBody};
      customOnUnload();
    }

And then the following to custom.js (for right now, just trying to display an alert to test that we're even going into the custom onUnload function):

// Called from onUnload function that is generated by AjaxSwing
function customOnUnload()
{
    window.alert('Hello World');
}

Starting with this, we received an error when trying to load the page regarding a "problematic instruction".  See attached debug.

We then removed the "problematic instruction" and we can go into the page just fine, but, on exiting the page, it doesn't appear that customOnUnload() is being invoked.

Can you please offer some further assistance?

Thank you.




Support Team

  • Administrator
  • Hero Member
  • *****
  • Posts: 1074
    • View Profile
Re: OnUnload
« Reply #3 on: May 29, 2009, 04:56:35 PM »
It definitely works, I have just tried it myself.

I have added this to AjaxSwing/tomcat/webapps/ajaxswing/scripts/custom.js:

function customOnUnLoad()
{
   alert('custom on unload');
}

And made this change in the page-header.html

<body onLoad="onLoad()" class="${page.cssClass}" onUnload="customOnUnLoad()">

When I navigated away from AjaxSwing page I get the alert.

In your case, what is ${page.onUnloadScriptBody}? There is no such object defined in FreeMarker context so you can't refer to it. I assume this is a copy/paste from onLoad and probably that's why you were getting the error. Remove it, double check that you didn't misspell function names, clear browser cache and enjoy the functionality:-) Use Firebug console to see if there are any errors and that cached scripts are updated.


aevett

  • Customer
  • Sr. Member
  • *
  • Posts: 482
    • View Profile
Re: OnUnload
« Reply #4 on: June 01, 2009, 06:07:25 PM »
We did get OnUnload to work, but unfortunately it doesn't quite fill our need.  This is mainly because OnUnload not only gets called when the user goes to a new web page, but also when they use the navigation menu in the portal to navigate away from the embedded AjaxSwing IFrames to the other embedded IFrame or another page.

So we are stuck.  You may recall we had to put in special rules to know when a request is for freemarker as opposed to ajaxswing.  A request is only forwarded to freemarker if it's a new session or the request ends in .htm or .html.  Since we can't use OnUnload to clear the cookie sessionID, we never think it's a new session when they come back to the portal page from another site.

Any ideas?

Support Team

  • Administrator
  • Hero Member
  • *****
  • Posts: 1074
    • View Profile
Re: OnUnload
« Reply #5 on: June 01, 2009, 06:30:22 PM »
There are several options but it would be best for us to understand exactly what you are trying to accomplish. Some ideas are:

If IFRAME switching from the menu is causing problem, you can call JavaScript before/after IFRAME switching to add/remove cookies. Instead of following a link or setting window.location, call JavaScript function that will do setup work and then set window.location. For instance, that JavaScript function can set a global variable flag indicating to onUnload handler that unload event should be ignored and cookie kept.

Are you trying to prevent the user from being able to return to the session if he navigated away from your portal? Normally web sites don't react to such events, so can you just leave this whole functionality out, and rely on session expiration? As long as the user returns before the session times out, he will be able to continue, otherwise you know the cookie is for invalid session and you just ignore it.

If this didn't help, then this may be a topic more suitable for a phone conversation. Send us an email and we'll setup a conference call.


Support Team

  • Administrator
  • Hero Member
  • *****
  • Posts: 1074
    • View Profile
Re: OnUnload
« Reply #6 on: June 02, 2009, 11:10:39 PM »
Glad you got this to work. We've released AjaxSwing 2.4.2 today with the latest bug fixes and patching utility. Be sure to download it when you get a chance.


kdiamond

  • Customer
  • Jr. Member
  • *
  • Posts: 53
    • View Profile
Re: OnUnload
« Reply #7 on: June 04, 2009, 03:44:13 PM »
We installed 2.4.2 and tested.  All is fine now.

Thank you!