May 14, 2000 - Clicking Away Windows | WebReference

May 14, 2000 - Clicking Away Windows

Yehuda Shiran May 14, 2000
Clicking Away Windows
Tips: May 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

You have probably noticed applications presenting an About box that closes when the user clicks the mouse anywhere in the box. We'll use JavaScript to apply the same effect to a window. First, here's the code that opens the new window:

<SCRIPT LANGUAGE="JavaScript">
<!--
function launchAbout() {
  about = window.open("about.html", "about", "height=75,width=250");
  return false;
}
// -->
</SCRIPT>
<A HREF="about.html" onClick="return launchAbout()">About</A>

You can close the new window by clicking anywhere inside it. We use the following script in the HEAD portion of the about.html document:

<SCRIPT LANGUAGE="JavaScript">
<!--
function closeWin() {
  window.close();
}
if (window.Event) document.captureEvents(Event.ONCLICK);
document.onclick = closeWin;
// -->
</SCRIPT>

For more information about events, refer to Column 9, The Navigator Event Model, Column 10, The Internet Explorer Event Model, and Column 11, The Cross-Browser Event Model. For a similar keyboard implementation, see Hot Keys (November 24, 1999). For a live demo, go to Tutorial 1.