When a user clicks the x in a window to close an application, which os feature is invoked?

Ordinary javascript cannot close windows willy-nilly. This is a security feature, introduced a while ago, to stop various malicious exploits and annoyances.

From the latest working spec for window.close():

The close() method on Window objects should, if all the following conditions are met, close the browsing context A:

  • The corresponding browsing context A is script-closable.
  • The browsing context of the incumbent script is familiar with the browsing context A.
  • The browsing context of the incumbent script is allowed to navigate the browsing context A.

A browsing context is script-closable if it is an auxiliary browsing context that was created by a script (as opposed to by an action of the user), or if it is a browsing context whose session history contains only one Document.

This means, with one small exception, javascript must not be allowed to close a window that was not opened by that same javascript.

Chrome allows that exception -- which it doesn't apply to userscripts -- however Firefox does not. The Firefox implementation flat out states:

This method is only allowed to be called for windows that were opened by a script using the window.open method.


If you try to use window.close from a Greasemonkey / Tampermonkey / userscript you will get:
Firefox: The error message, "Scripts may not close windows that were not opened by script."
Chrome: just silently fails.


The long-term solution:

The best way to deal with this is to make a Chrome extension and/or Firefox add-on instead. These can reliably close the current window.

However, since the security risks, posed by window.close, are much less for a Greasemonkey/Tampermonkey script; Greasemonkey and Tampermonkey could reasonably provide this functionality in their API (essentially packaging the extension work for you).
Consider making a feature request.


The hacky workarounds:

Chrome is currently was vulnerable to the "self redirection" exploit. So code like this used to work in general:

open(location, '_self').close();

This is buggy behavior, IMO, and is now (as of roughly April 2015) mostly blocked. It will still work from injected code only if the tab is freshly opened and has no pages in the browsing history. So it's only useful in a very small set of circumstances.

However, a variation still works on Chrome (v43 & v44) plus Tampermonkey (v3.11 or later). Use an explicit @grant and plain window.close(). EG:

// ==UserScript==
// @name        window.close demo
// @include     http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant       GM_addStyle
// ==/UserScript==

setTimeout (window.close, 5000);

Thanks to zanetu for the update. Note that this will not work if there is only one tab open. It only closes additional tabs.


Firefox is secure against that exploit. So, the only javascript way is to cripple the security settings, one browser at a time.

You can open up about:config and set
allow_scripts_to_close_windows to true.

If your script is for personal use, go ahead and do that. If you ask anyone else to turn that setting on, they would be smart, and justified, to decline with prejudice.

There currently is no equivalent setting for Chrome.

Lesson 9: Understanding Applications

/en/computerbasics/understanding-operating-systems/content/

What is an application?

You may have heard people talking about using a program, an application, or an app. But what exactly does that mean? Simply put, an app is a type of software that allows you to perform specific tasks. Applications for desktop or laptop computers are sometimes called desktop applications, while those for mobile devices are called mobile apps.

When you open an application, it runs inside the operating system until you close it. Most of the time, you will have more than one application open at the same time, which is known as multi-tasking.

App is a common term for an application, especially for simple applications that can be downloaded inexpensively or even for free. Many apps are also available for mobile devices and even some TVs.

Watch the video below to learn more about applications.

Looking for the old version of this video? You can still view it here.

Desktop applications

There are countless desktop applications, and they fall into several categories. Some are more full featured (like Microsoft Word), while others may only do one or two things (like a clock or calendar app). Below are just a few types of applications you might use.

  • Word processors: A word processor allows you to write a letter, design a flyer, and create many other types of documents. The most well-known word processor is Microsoft Word.

    When a user clicks the x in a window to close an application, which os feature is invoked?

  • Web browsers: A web browser is the tool you use to access the Internet. Most computers come with a web browser pre-installed, but you can also download a different one if you prefer. Examples of browsers include Internet Explorer, Mozilla Firefox, Google Chrome, and Safari.

    When a user clicks the x in a window to close an application, which os feature is invoked?

  • Media players: If you want to listen to MP3s or watch movies you've downloaded, you'll need to use a media player. Windows Media Player and iTunes are popular media players.

    When a user clicks the x in a window to close an application, which os feature is invoked?

  • Games: There are many types of games you can play on your computer. They range from card games like Solitaire to action games like Halo. Many action games require a lot of computing power, so they may not work unless you have a newer computer.

Mobile apps

Desktop and laptop computers aren't the only devices that can run applications. You can also download apps for mobile devices like smartphones and tablets. Here are a few examples of mobile apps.

When a user clicks the x in a window to close an application, which os feature is invoked?

  • Gmail: You can use the Gmail app to easily view and send emails from your mobile device. It's available for Android and iOS devices.
  • Instagram: You can use Instagram to quickly share photos with your friends and family. It's available for Android and iOS.
  • Duolingo: With a combination of quizzes, games, and other activities, this app can help you learn new languages. It's available for Android and iOS.

Installing new applications

Every computer and mobile device will come with some applications already built in, such as a web browser and media player. However, you can also purchase and install new apps to add more functionality. You can review our lessons on Installing Software on Your Windows PC, Installing Software on Your Mac, and Free Software to learn more.

When a user clicks the x in a window to close an application, which os feature is invoked?

/en/computerbasics/setting-up-a-computer/content/

Which feature of an OS provides a method for users to interact with the computer?

User interface - The features of a computer system which allows the user to interact with it. A user interface, also sometimes called a human-computer interface, comprises both hardware and software components. It handles the interaction between the user and the system.

Which OS is used the most on modern user desktop computers in business environments?

For desktop and laptop computers, Windows is the most used at 74%, followed by Apple's macOS at 14%, and Linux-based operating systems, at 5% (i.e. "desktop Linux" at 2.84%, plus Google's ChromeOS at 2%, in the US up to 4.43%).

What happens on a computer that runs a cooperative multitasking OS?

Cooperative multitasking is a type of computer multitasking in which the operating system never initiates a context switch from a running process to another process. It interrupts applications and gives control to other processes outside the application's control.

What is preemptive multitasking in OS?

Preemptive multitasking is a type of multitasking that allows computer programs to share operating systems (OS) and underlying hardware resources. It divides the overall operating and computing time between processes, and the switching of resources between different processes occurs through predefined criteria.