14 out of 29 rated this helpful - Rate this topic

close method

[This documentation is preliminary and is subject to change.]

Closes the current browser window or HTML Application (HTA).

Closes the current window.

Syntax

var retval = window.close();

Standards information

There are no standards that apply here.

Parameters

This method has no parameters.

Return value

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Remarks

When a function fired by an event on any object calls the close method, the window.close method is implied.


<SCRIPT LANGUAGE="JScript">
function myClose() {
    close();}
</SCRIPT>
<BODY onclick="myClose();">
Click this page and window.close() is called.
</BODY>

When an event on any object calls the close method, the document.close method is implied.


<BUTTON onclick="close();">
Click this button and document.close() is called.
</BUTTON>

How a window is closed programmatically determines whether the user is prompted with a confirmation dialog box:

  • Invoking the window.close method on a window not opened with script displays a confirmation dialog box. Using script to close the last running instance of Windows Internet Explorer also opens the confirmation dialog box.
  • Invoking the window.close method on an HTA closes the application without prompting the user because the HTA is trusted and follows a different security model. For more information on the security model of HTAs, please refer to The Power of Trust: HTAs and Security.

Invoking the window.close method on a Metro style app using JavaScript closes the window without prompting the user. The last window to close closes the application.

 

 

Build date: 3/8/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
window.close() and xslt
When a window is opened with script and the url contains xml content parsed to html by xslt, the close() command will still fire the confirmation dialog box!

MAG: Is there a workaround for this? How to close a window in this manner on a page parsed by xslt in script without getting the confirmation dialog box?

Yes! You can bypass this bug by replacing the current window with a standard html page that just closes itself: eg, in your xslt transfromed page instead of self.close() use window.open('ieclose.htm','_self');

ieclose.htm looks like:

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;meta http-equiv="cache-control" content="no-cache" /&gt;
&lt;meta http-equiv="pragma" content="no-cache" /&gt;
&lt;meta http-equiv="expires" content="-1" /&gt;
&lt;title&gt;closing&lt;/title&gt;
&lt;/head&gt;
&lt;body onload="self.close()"&gt;
&lt;/body&gt;
&lt;/html&gt;

You can also try:
window.open('','_self');window.close();

It worked for me.

SCOTT:
Here's a clever one-line workaround (a combination of the two above items) that I've been using for years:
window.open("javascript:'<script>window.close()</script>'", "_self");
...
...
META tag Refresh Method
META tag Refresh Method

none of the above methods worked for me,
but success if I did a:

<META
HTTP-EQUIV="Refresh"
CONTENT="1; URL=webclose.html">

...and then created webclose.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="expires" content="-1" />
<title>closing</title>
</head>
<body onload="self.close();">
</body>
</html>

...go figure. I'm guessing the 1 sec delay lets the client browser better assimilate the requested windows management.