JavaScript & HTML Tips
 
Feature highlight: Window Closing
Credit: Imagenation


Description
Controling window closings - somewhat.


Background
It is sometimes very important to control the opening and closing of windows especially when users have filled in forms - because even if the user returns immediately to the page, all the filled in data is lost unless your program provides recovery.

Key features and CSS elements
By
using the onUnload event in the <body> tag and a small closeout function developers can prevent users from inadvertently following a link that will cause havoc. However, if the user closes down the browser using the exit or cancel icon - these situations are not controllable by these methods.

Again the code is fairly simple:

function closeOut(){
  if (confirm("Do you really want to close this window")){
    return true; }
   location.href = document.URL;;
  return true;
}

First we put in a confirm dialog which warns the user and then awaits a response. If the user clicks Ok the window is unloaded; however if the user clicks cancel then the location.href - where to go next - is set to this page again through the document.URL effectively canceling the change. WARNING - even this may not preserve the forms variables in some browsers.

 
Top of Page  Tutorials Home  JavaScript References  JavaScript Books