prevent web page from closing before it has finished loading

Posted on 2007-11-30
Medium Priority
Last Modified: 2009-06-12
I have a web page that is opening a connection to a crystal report. When the report window is closed, I am closing the connection. However, it is possible for the user to close the window with the x before it finishes loading, in which case the onunload event never fires, which is where I'm closing the connection. Is there a way to prevent the user from closing the window before it finishes. I'm using a synchronous AJAX call to close the connection. An asynchronous call fails because the calling window is destroyed before it finishes.
Question by:kclilo
LVL 54

Accepted Solution

b0lsc0tt earned 2000 total points
ID: 20386366

No there isn't.  The onunload event is the closest thing offered by html and browsers.  As you have found out that won't fire when the browser is just closed.

Are the users doing this on purpose, despite a notice of the problem or is it just innocent.  Most likely they just don't realize they need to leave it open.  The common fix for this is a clear message stating that the browser needs to stay open or the process will fail.  If the process is real long then people will become impatient but you could redesign the process or break it in parts so the "wait" is not a cause of this.  If the issue is just open sessions or other server stuff then you might consider scripts or procedures to clean the DB, etc up as the server ends the session.

The basic answer though is No.  You can't stop it or even detect it directly.  i do hope the info above will give you some ideas though.  Let me know if you have any questions or need more information.

LVL 29

Expert Comment

ID: 20386377
can you not catch the close window command and shutdown gracefully?
LVL 101

Expert Comment

ID: 20386476
Can you get the window id or handle and check if it exists before trying to shut it down.

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.


Author Comment

ID: 20386738
The trouble is, this is a web browser window, how do you intercept when it's trying to shutdown.  I fear that b0lsc0tt is correct, but hasn't provided any solution other than warn the user not to close the page, which I cannot do in this case.
LVL 36

Expert Comment

ID: 20387317
I am not familiar with connecting to Crystal Reports, but is it through server code? If so, it does not automatically release the connection if the connection is lost?
LVL 28

Expert Comment

ID: 20387423
If I understand you well, then I don't see the problem.

Each page is a separate application. To the server each (asynchronous) call made by the javascript is a separate application, which requires its own connection to be opened and closed.

You can not send a connection along with an asynchronous javascript call. You can only send some parameters.

So each page that you create and which uses a connection should first open a connection, do things with it and then close the connection again. You can not close a connection by an asynchronous javascript call.

Maybe I misunderstand the way you are working, but if that is so, I am very much interested in how you manage to close a connection which was opened by a different call from the client.

Author Comment

ID: 20388412
Thanks to everyone that is trying to help me solve this.  Here are some more details to help clarify the problem. When I say connection, I don't mean a DB connection, I'm talking about a logon to the Crystal XI report server. The logon takes place in a JSP page and is a call to the server which returns an enterpriseSession object. After logon a report is returned in a Crystal Viewer page which is wrapped in an open.window() call. The enterpriseSession object is stored in a session variable and is accessible when the window closes by doing window.onunload=doLogoff(). This all works fine unless the user closes the window before the report has finished loading. This happens because the logon has been established to Crystal, but the onunload code never gets called to logoff from Crystal. Because of the way the licensing works, we are trying to logon and logoff for each report viewing so that concurrent users can run reports without getting locked out. I don't know why Crystal is not able to release the enterpriseSession when the report window is closed. The only way it will get released is after a 15-20 minute wait. It's looking like I'll have to monitor those sessions by user/pwd and release/reuse them  myself if the same user tries to access another report. It just seems like there should be an easy way to prevent the user from closing the window before it finishes loading, or fire my logoff code regardless, which would solve the problem. Thanks again for you help!
LVL 36

Expert Comment

ID: 20388449
Poke around a bit and see if there is any way to set a timeout less than 15-20 minutes for inactivity. If not, you can keep track of it yourself through a table in your database. If you have enough users, you can log other people off for inactivity through your other users' activities.

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

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.

Join & Write a Comment

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question