469,140 Members | 1,388 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,140 developers. It's quick & easy.

onbeforeunload and servlets

Hello:

Can anybody tell me how to detect browser close event in Javascript and how
to call a servlet on a Brower close?

I tried some thing:

<html>
<SCRIPT LANGUAGE="JavaScript1.2" TYPE="text/javascript">

function unloadMess(){
mess = "message."
return mess;
}

function setBunload(on){
window.onbeforeunload = (on) ? unloadMess : null;
location.href="http://localhost/servlet/ServletPrueba";
}

setBunload(true);

</SCRIPT>
<BODY>
<a href="http://www.microsoft.com" onClick="setBunload(true)">Link 1</a>
<br>
<a href="http://localhost:8080/Version3/pagina2.html"
onClick="setBunload(false)">Link 2l</a>
</BODY>
</html>

But, I want call the servlet only when close browser or I browse to another
web.

Thanks.
Jul 23 '05 #1
1 4143
Derek wrote:
Hello:

Can anybody tell me how to detect browser close event in Javascript and how
to call a servlet on a Brower close?

I tried some thing:

<html>
<SCRIPT LANGUAGE="JavaScript1.2" TYPE="text/javascript">

function unloadMess(){
mess = "message."
return mess;
}

function setBunload(on){
window.onbeforeunload = (on) ? unloadMess : null;
location.href="http://localhost/servlet/ServletPrueba";
}

setBunload(true);

</SCRIPT>
<BODY>
<a href="http://www.microsoft.com" onClick="setBunload(true)">Link 1</a>
<br>
<a href="http://localhost:8080/Version3/pagina2.html"
onClick="setBunload(false)">Link 2l</a>
</BODY>
</html>

But, I want call the servlet only when close browser or I browse to another
web.


You are trying to "close down" your servlet session to release resources
when the user terminates the browser, or navigates away?

There's no *reliable* way of doing this, but where I work, we've had to
do this because of "per seat" licensing in the back end, and the
connection which is maintained in the servlet session. (Yes, it's
horrible, I inherited it, the back end server maintains a huge amount of
state for each connection, and they don't want to set it up and tear it
down for every request)

We've had to do something VERY ugly...

The application is loaded into a frameset, so the front page just
contains (bear with me, I've forgotten the attributes...)

<frameset ununload="window.open('logout.do','','height=100,w idth=100')"
frameborder="no" rows="*">
<frame src="TheREALfrontpage.jsp" border="0">
<noframes>Tough! We've implented an ugly hack</noframes>
</frameset>

All subsequent pages load into the frame, so the unload event isn't
fired until you close thr browser of navigate that window away.

So, you get a little window popping up when you close the browser or
navigate away.

The logout servlet must send

<script type="text/javascript">
self.close();
</script>

which then gets rid of it.

Of course this is riddle with flaws. If the user opens any of the links
in the application in a new window, these don't have that extra frame.
The use may close the original frame which will log out, and invalidate
the session. The newly opened windows will be orphaned.

Yuck. Horrible. I've tried to implement all kind of other ways.

One way was not using the frame and having *every* page in the app fire
a logout request on unload. The servlet would flag a request for another
page, and ignore that logout if another page in the app was being requested.

Well, on a page *refresh", the onunload is fired before the request is sent.

There is no good way to do this.

Nige

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by lltaylor | last post: by
6 posts views Thread by jennyw | last post: by
4 posts views Thread by sowencheung | last post: by
8 posts views Thread by drewmania001 | last post: by
1 post views Thread by David | last post: by
3 posts views Thread by Bit Byte | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.