最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Waitsleep before window.close() - Stack Overflow

programmeradmin0浏览0评论

I try to set up a simple logoff-page which should close after 3 seconds.

Regarding this Question is it possible to wait before that action?

<script type="text/javascript">
sleep(3000);
window.open('', '_self', ''); 
window.close();
</script>

With the sleep nothing happens at all.

Edit:

The solution of @Sidius works well in IE without a prompt.

Unfortunately Firefox blocks the Script:

Scripts can not close any windows that were not opened by the script

I try to set up a simple logoff-page which should close after 3 seconds.

Regarding this Question is it possible to wait before that action?

<script type="text/javascript">
sleep(3000);
window.open('', '_self', ''); 
window.close();
</script>

With the sleep nothing happens at all.

Edit:

The solution of @Sidius works well in IE without a prompt.

Unfortunately Firefox blocks the Script:

Scripts can not close any windows that were not opened by the script

Share Improve this question edited May 23, 2017 at 12:07 CommunityBot 11 silver badge asked Dec 3, 2015 at 15:17 BlueFoxBlueFox 1711 gold badge2 silver badges13 bronze badges 5
  • 1 window.setTimeout(function(){},time);? – www139 Commented Dec 3, 2015 at 15:19
  • Are you saying that the new window opens, it waits 3 seconds and then closes? – www139 Commented Dec 3, 2015 at 15:21
  • Does that hack to close a browser you did not open really still work? – epascarello Commented Dec 3, 2015 at 15:23
  • @www139 nothing happens. – BlueFox Commented Dec 3, 2015 at 15:42
  • Check my edited answer @BlueFox – Sidius Commented Dec 3, 2015 at 15:51
Add a ment  | 

3 Answers 3

Reset to default 4

try this:

window.open('', '_self', '');
setTimeout(function(){
   window.close(); 
}, 3000);

Edit: I think firefox might be a little more strict with the

window.open()

function. You might want to give values to the function's constructor.

window.open(URL,name,specs,replace); 

In example:

window.open("", "", "width=200, height=100");

You can use setTimeout() API to achieve the same -

setTimeout(window.close,3000);

You need to use window.setTimeout():

window.open('', '_self', '');
// Add this instead.
setTimeout(function(){
   window.close(); 
}, 3000);
发布评论

评论列表(0)

  1. 暂无评论