devxlogo

Why doesn’t the window.close() method always work?

Why doesn’t the window.close() method always work?

Question:
Why doesn’t the window.close() method always work?

Answer:
In JavaScript 2.0, the window.close() method could be used to close any open browser window. People being the malicious animals that they are, the window.close() command was exploited in practical jokes. With JavaScript 2.0, jokers were able to rig their Web pages so that a guest’s browser would automatically close when he or she stumbled upon the joker’s Web page. This was never a threat to anyone’s system, but it was still annoying because it caused users to “lose their place” on the Web. (Closing a browser window destroys its history list.)

JavaScript 2.0 also made it possible to send JavaScript “mail bombs” to users who used Netscape Mail. When opened, these mail bombs would automatically cause the user’s mail window to close. Not only would this leave the unlucky recipient inconvenienced and confused, it might also lead him or her to believe that there was a bug or virus in the software.

Netscape addressed this problem in JavaScript 3.0 by restricting the ways in which window.close() can be used. In particular, the new specification says that only windows which were opened with JavaScript can be closed by JavaScript.

On some systems, when JavaScript is used to close a window that it didn’t open, the user is presented with a confirmation box warning that the window is about to be closed. At this point, the user is given an opportunity to cancel the operation.

If you are using JavaScript 3.0, you can see this new security feature in action by pressing the button below. (Caution: If you are using JavaScript 2.0, pushing this button will close your browser window!)

There is one exception to the rule mentioned above. If the browser window has only one item in its history list, JavaScript can close the window without the user’s confirmation. Netscape’s documentation says that this is a “special case for one-off windows that need to open other windows and then dispose of themselves.”

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist