开发者

JS Close window

Is there any way to close the browser window completely using js without receiving a prompt? I m开发者_StackOverflowean a browser window that a user opened not a pop-up or anything else


No. Basically, only a window that a script opened may be closed by a script. Trying to do so (using the command window.close()) results in the error Scripts may not close windows that were not opened by script.

There is a good reason for this. If any script could close any window, malicious code could easily substitute a fake page for a legit page by loading the fake page in a background window then closing the main window.

Generally speaking, it is bad practice to try and force control of any actions that are usually the domain of your user. This includes things like scroll bars, overall font size, confirmation windows (for printing, saving, closing), and size and state of open windows, etc.


the short answer is NO. Typically all browsers have mechanisms of configuring what to allow JavaScript code to do -- closing windows and/or resizing is one of them and by default is set to prompt (for closing) and deny for resizing. So unless the user has specifically configured a different level, you won't be able to avoid the prompt.


The only code that exists to close windows is window.close(). For anything else, you'll need superior previleges like the addons have.


window.close() will close the current tab, and if it's the last tab of the browser, it might close the whole browser window, too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜