javascript 页面完全加载后关闭窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26888780/
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-10-28 06:38:28  来源:igfitidea点击:

Closing a window after the page is fully loaded

javascriptfirefox

提问by user3636583

I am trying to write a JavaScript function that will work on Firefox 5.0. I need the page to fully load, and then close. What I'm trying to do is:

我正在尝试编写一个适用于 Firefox 5.0 的 JavaScript 函数。我需要页面完全加载,然后关闭。我想要做的是:

var temp = window.open(link);

temp.window.onload = function () {
    temp.window.close();
}

But so far all it does is open the new tab, but doesn't close it.

但到目前为止,它所做的只是打开新选项卡,但没有关闭它。

Is there any way to successfully accomplish this?

有什么办法可以成功地做到这一点吗?

采纳答案by Jure

You could maybe create new js file for that window and have just window.close(); inside.

您可以为该窗口创建新的 js 文件,并且只有 window.close(); 里面。

回答by epascarello

First if the link is not in the same domain, you will not be able to close the window because of the same origin policy.

首先,如果链接不在同一个域中,由于同源策略,您将无法关闭窗口。

Listens for the onload event with addEventListener

使用 addEventListener 监听 onload 事件

var temp = window.open(link); 
temp.addEventListener('load', function() { temp.close(); } , false);

if you need to support old IEs than you would need to attachEvent

如果您需要支持旧 IE,则需要附加事件

var temp = window.open(link); 
temp[temp.addEventListener ? 'addEventListener' : 'attachEvent']( (temp.attachEvent ? 'on' : '') + 'load', function() { temp.close(); }, false );


There is no need to open up a window to hit a web page.

无需打开窗口即可访问网页。

You can:

你可以:

  • Make an Ajax request - must be same domain
  • Set a hidden iframe
  • Set an image source
  • 发出 Ajax 请求 - 必须是同一个域
  • 设置隐藏的 iframe
  • 设置图像源

回答by Leysam Rosario

If you have a access to the popup window you can add this:

如果您有权访问弹出窗口,则可以添加以下内容:

jQuery

jQuery

<script>
    $(window).load(function(){
        open(location, '_self').close();
    });
</script>

Javascript

Javascript

<script>
  window.onload = function () {
     open(location, '_self').close();
   };
</script>

I also suggest you read this Questionand this answer

我还建议你阅读这个问题和这个答案

回答by betrice mpalanzi

you can use something like

你可以使用类似的东西

function closed(){
    setTimeout("window.close()", 6000);
}