Home >Web Front-end >JS Tutorial >Solve the problem that javascript:window.close() fails in chrome and Firefox_javascript skills

Solve the problem that javascript:window.close() fails in chrome and Firefox_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:34:431760browse

Window.close(), at first glance, you can tell that it is a method used to close the browser window. W3CSchool explains this method as follows: The method close() will close the top-level browser window specified by window. A window can close itself by calling self.close() or just close(). Only windows opened by JavaScript code can be closed by JavaScript code. This prevents malicious scripts from terminating the user's browser.

In IE, window.close() can take effect. When it takes effect, a prompt box will pop up asking you whether to close the window, as shown in the figure :

 

Click "Yes" to close the window directly. But this code will not take effect under chrome and Firefox. For the Chrome browser, you can write the following code to make it effective:

Copy the code The code is as follows:

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

First open a page in this window through window.open(), Then close it through the window.close() method. This fulfills the saying: "Only windows opened by JavaScript code can be closed by JavaScript code."

This code is valid for both IE and chrome, but it is invalid under firefox. You need to perform the following operations on firefox:

1. Enter about:config

in the Firefox address bar

2. Find dom.allow_scripts_to_close_windows in the configuration list
3. Right-click the switch and change the above false to true.
Note: The default is false to prevent scripts from closing the window randomly

After setting this in FireFox, directly use "window.close()" to close the window.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn