Problem with Onunload event.

Recently I put a question regarding HttpSession invalidation when user jumps to otherURL. I got a answer but still I am facing some problems.

What I did was,  I have two frames in one page. In one of the frame I have coded java script like this.

                 <script language="JavaScript">
                 var leaving = false;

 <body onUnload='if (!leaving) document.forms["search"].submit()'>
                 <form name=search action="/servlet/Logout" method="GET">
                 <input type="hidden" name="Invalidate" value="true">

                  I have servlet(Logout)  which Invalidate the HttpSession.

The problem is,  When I jump to other URL like or from my application/website  the onUnload event is invoking the servlet and It displaying a empty page as responce from the server. In my browser location  also It displaying my servlet path.

But I don't want this.  onUnload has to invoke the servlet and at  the same time the or page has to be displayed. I should not get any responce from my servlet.

Is there any way to do this?

Thanks in advance.

Who is Participating?
Jan LouwerensConnect With a Mentor Software EngineerCommented:
oh wow... I'm glad my sparse reflections actually helped =)
do you have any way of knowing what the destination URL is in your servlet? or maybe even in the javascript in the onUnload function?
Jan LouwerensSoftware EngineerCommented:
I'm pulling this all from memory, so please forgive me if I'm off, but I remember there being codes you could return that specifies what the result of the servlet does (for example 404 is one of those codes, 404 being page not found). There are codes that specify that there are no results from the servlet, and even ones that specify that the page should not be redrawn after calling the servlet
gnagabandiAuthor Commented:
Hi jlouwere, thank you. It is working. I coded like this.

//No content for 204.

It does not return enything.  But  still I could not able resolve my entire problem..
Say I jumped to MSN.COM from my application. During this process the onUnload event invoking session invalidate program and there is nothing displayed ( with the help of above code)  in the browser window. In the browser location the URL is still there. If I hit enter one more time  only,  the msn homepage is displayed.

The first hit itself has to display the msn home page.  Any help will be appreciated.

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

gnagabandiAuthor Commented:
I tried with window.location.url but it capturing the application url (like getRemoteHost), not the destination URL.

Jan LouwerensSoftware EngineerCommented:
hmmmm... I dunno... perhaps you can try this:

in you onUnload function, create a new small HTML document with the form in it and use onLoad to do the same thing as the onUnload does now. in your current onUnload, use to creat a new window with the new HTML file with the form in it. (which will submit the form as soon as it loads). Then, perhaps, to clean up, you can make the servlet actually return some HTML with javascript that can close the window...

I know this is kinda complicated, and an ugly hack, but it just might work...
gnagabandiAuthor Commented:

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.