This sample page demonstrates how to load a page with initial information, then when a button (or link) is clicked, it updates a database via ColdFusion, briefly displays a message, then closes itself. The main problem is that you cannot put both a "submit document" line and a "close window" line in the same JavaScript function because only the line that is processed first will be executed.. E.g.:

function escape_window() {
     document.form.submit();
     self.close(); }

Here, because the page is reloaded when the form is submitted, it apparently cancels the self.close command because it is essentially a "new" page. A second example, where the lines are reversed, closes the window before the document submission command has a chance to activate. This is true even if you set a delay on the window closing:

function escape_window() {
     setTimeout("self.close();",3000)
     document.form.submit(); }

There appears to be two ways that I found to do this:

1) <form onSubmit="setTimeout('window.close',5000)">
    <input type="submit" value="Close Window">
    </form>

Because JavaScript has no way of knowing when the form has finished beinging submitted - a guesstimate of the amount of time required for the form submit is chosen (5 seconds) and a time is set up to close the window after 5 seconds has passed. HOWEVER, on a slow day the form may still not have been submitted within the 5 seconds, the closing of the window will interrupt the form submission and it will fail.

2) A way to avoid the pitfall of option #1 is to use a ColdFusion conditional to set the page's body tag:

<cfif NOT IsDefined("form.hidden")>
     <cfset body_code = '<body bgcolor="##FFFFFF" text="##000000">'>
<cfelse>
     <cfset body_code = '<body bgcolor="##FFFFFF" text="##000000" onLoad="javascript:self.close();">'>
</cfif>

Initially, the first condition is met because "form.hidden" is not yet defined. The "form.hidden" field is passed when the form is submitted upon clicking the button/link. It and the rest of the form is passed to the same page, which causes the page to be reloaded. This time the second condition will be met, which means the JS command for the window to close itself will be run upon it being loaded. However, because CF all the CF is processed before any JS code is run, the CF code updating the database (whose activation is also a condition of "form.hidden" being defined) will be activated before the window closes itself.

To see an example of this, click here.