是否可以使用 javascript 关闭浏览器窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5455426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:15:59  来源:igfitidea点击:

Is it possible to close a browser window using javascript?

javascript

提问by ashishjmeshram

I need to close the browser window using the java script.

我需要使用 java 脚本关闭浏览器窗口。

I have tried using window.close but it is not working.

我曾尝试使用 window.close 但它不起作用。

I have also googled and checked stack overflow for any similar posts. I have also gone through following link but in vain.

我还用谷歌搜索并检查了任何类似帖子的堆栈溢出。我也经历了以下链接,但徒劳无功。

Is it possible to close browser window from JavaScript?

是否可以从 JavaScript 关闭浏览器窗口?

What I am trying to do is load a page and on load of the page call a java script method which does window.close

我想要做的是加载一个页面并在页面加载时调用一个执行 window.close 的 java 脚本方法

Below is the code

下面是代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script>
function closeBrowser(){
    window.close();
}
</script>
</head>
 <body onload="closeBrowser();">
 </body>
</html>

It is properly working in IE8 though. Its not working in the fire fox.

不过它在 IE8 中正常工作。它在火狐中不起作用。

And even in IE it gives the alert prompt before actually closing the browser. Can we avoid the alert in IE?

即使在 IE 中,它也会在实际关闭浏览器之前给出警报提示。我们可以避免 IE 中的警报吗?

Why it does not work in fire fox?

为什么它在火狐中不起作用?

回答by ashishjmeshram

The reason it was not working in fire fox was due to some settings in the fire fox.

它在火狐中不起作用的原因是由于火狐中的某些设置。

Please set your firefox browser:

请设置您的火狐浏览器:

1.input "about:config " to your firefox address bar and enter;

1.在你的firefox地址栏输入“about:config”并回车;

2.make sure your "dom.allow_scripts_to_close_windows" is true

2.确保你的“dom.allow_scripts_to_close_windows”是真的

window.close is now working in fire fox too.

window.close 现在也在火狐中工作。

回答by Carl R

You have the first script tag twice, which might cause problems. You should also add the attribute type="text/javascript" so that you can be sure that the script is picked up by all browsers.

您有两次第一个脚本标记,这可能会导致问题。您还应该添加属性 type="text/javascript" 以便您可以确保所有浏览器都选择该脚本。

回答by mplungjan

What you want to do is undesirable for the user.

您想要做的事情对用户来说是不受欢迎的。

If a user loads your page and you as a webmaster decides that the main browser needs to close, then they will likely get seriously annoyed if you succeeded. Window history and such, gone.

如果用户加载了您的页面,而您作为网站管理员决定需要关闭主浏览器,那么如果您成功,他们可能会非常生气。窗口历史之类的,消失了。

Please explain EXACTLY why you want this. For example if you need to log someone out use location.replace('logout.php)` and have that page log them out

请准确解释你为什么想要这个。例如,如果您需要将某人注销,请使用location.replace('logout.php)` 并让该页面将他们注销

There are hacks that I personally wish would be fixed - in this exact duplicate here: How can I close a browser window without receiving the "Do you want to close this window" prompt?

我个人希望修复一些黑客攻击 - 在此处完全重复:如何关闭浏览器窗口而不收到“您想关闭此窗口”提示?

回答by Maxim Dsouza

There a thousand things which work in IE but not in other browsers. I think window.close(); should server your purpose. It can be called on any event on your page.

有一千种东西可以在 IE 中运行,但不能在其他浏览器中运行。我认为 window.close(); 应该服务于你的目的。它可以在您页面上的任何事件上调用。

回答by Abdel Raoof

When using the closemethod you need to keep in mind an important thing: The closemethod closes only windows opened by javascript using the openmethod. Since you are trying to close the current window, you should use

使用该close方法时,您需要记住一件重要的事情:该close方法仅关闭由 javascript 使用该open方法打开的窗口。由于您正在尝试关闭当前窗口,因此您应该使用

 self.close();

Also, you should remember that, users will get a confirmation, asking the user to choose whether the window is to be closed or not

另外,你应该记住,用户会得到一个确认,要求用户选择是否关闭窗口