detecting browser close

asked 2015-07-07 23:33:39 +0800

robertkaren gravatar image robertkaren
77 7

updated 2015-07-10 04:39:22 +0800

I've read other posts about detecting browser or tab close and based on that added the code below to my app's main page. I see it is called by IE (I will leave Chrome for another discussion :) whether browser closes, tab closes, or if a new URL is typed in. However in the first 2 cases the logout button whose click is simulated doesnt do its job and send http post to a backend system. Even with the alert there to keep the tab open it still doesn't send it. No error message. The only other thing that happens after login is a redirect back to login page, which obviously doesn't happen. Does anyone have an explanation and/or workaround for why the http post doesn't run when tab or browser is closed? (The http post is really being sent by the server (right?) so is tomcat cutting it off?) thanks.

<script type="text/javascript">
zk.afterMount(function () {
    console.log('setting up unload bind.');
    zk.beforeUnload(function() {
        var logoutBtn = zk.Widget.$(jq('$logout')[0]);
        zAu.send(new zk.Event(logoutBtn, 'onClick'));
        alert('logging off EzTAB');
        //alert('main.zul: do before unload');
        return null;


logout button does:

public void doLogout(){
    System.out.println("doLogout: sending server logout");

and doLogout() does:

                    URL url = new URL(urlName);
                URLConnection conn = url.openConnection();
                OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                result = getResult(conn, reader, writer);
delete flag offensive retag edit


I played around, but I can't get it to work properly. JavaScript isn't mine strongest point. You can try to contact ZK and ask if someone could look at this question.

chillworld ( 2015-07-10 07:17:20 +0800 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2015-07-16 14:18:25 +0800

trojak gravatar image trojak

you did very good job

link publish delete flag offensive edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

1 follower



Asked: 2015-07-07 23:33:39 +0800

Seen: 43 times

Last updated: Jul 16 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More