Closing Windows

If you've opened a window called my_window and want to close it later in the script, use the close() method:

my_window.close();

You'll recall from Chapter 4 that the word window refers to the window containing the JavaScript. This means you can also use JavaScript to close the window that's actually running the JavaScript, like this:

window.close();

This is exactly how the About the Author window on the Book of JavaScript page works. If you view the source code on the page that loads into one of the help windows, you'll see it has a button toward the bottom, labeled Close Window. If that button were a link, the script would look like this:

<a href = "#" onClick = "window.close(); return false;">Close Window</a>

Figure 5-6 shows how to ...

Get The Book of JavaScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.