admin管理员组文章数量:1277875
I have two setTimeout after another. One to open a webpage in the window and another to close the window after a predetermine amount of time. However, after using setTimeout to open a webpage, the next setTimeout that close the window does not work. The setTimeout with windows.close by itself works fine.
I and trying to opening a window with a message; then open a website in the window after a predetermine amount of time, and then close the window after a couple of more seconds has passed. Here is my function to do this:
function showNews() {
news = window.open("", "NewsWindow", "width=900, height=700");
news.document.write("<p>The 'NewsWindow' will only appearing for 3 seconds.</p>");
tmot = setTimeout(function(){news.window.open('','_self')}, 2000);
tmot = setTimeout(function(){news.window.close()}, 5000);
}
I tried running on both Chrome and IE and both browser would only execute one of the setTimeout and not the other. Any advice and help would be greatly appreciated!
I have two setTimeout after another. One to open a webpage in the window and another to close the window after a predetermine amount of time. However, after using setTimeout to open a webpage, the next setTimeout that close the window does not work. The setTimeout with windows.close by itself works fine.
I and trying to opening a window with a message; then open a website in the window after a predetermine amount of time, and then close the window after a couple of more seconds has passed. Here is my function to do this:
function showNews() {
news = window.open("", "NewsWindow", "width=900, height=700");
news.document.write("<p>The 'NewsWindow' will only appearing for 3 seconds.</p>");
tmot = setTimeout(function(){news.window.open('http://www.yahoo.','_self')}, 2000);
tmot = setTimeout(function(){news.window.close()}, 5000);
}
I tried running on both Chrome and IE and both browser would only execute one of the setTimeout and not the other. Any advice and help would be greatly appreciated!
Share Improve this question edited Mar 10, 2023 at 16:12 isherwood 61.1k16 gold badges120 silver badges169 bronze badges asked Mar 25, 2014 at 22:56 user3461873user3461873 511 gold badge1 silver badge3 bronze badges 2- I tried this in Firefox (27) and IE (11), and it works fine. – Guffa Commented Mar 25, 2014 at 23:04
-
Note that you lose your reference to the first timeout when you set the return of the second to
tmot
. – Philip Commented Mar 14, 2019 at 22:42
4 Answers
Reset to default 3You'll better reopen it using parent window with the same parameters :
function showNews() {
news = window.open("", "NewsWindow", "width=900, height=700");
news.document.write("<p>The 'NewsWindow' will only appearing for 3 seconds.</p>");
tmot = setTimeout(function(){window.open('http://www.yahoo.', "NewsWindow")},2000);
tmot = setTimeout(function(){news.close()}, 5000);
}
Not try on IE, i don't have it.
Some browsers forbid you from closing the window once it loads a website.
The following works:
HTML:
<button onclick="showNews()">Show News</button>
JavaScript:
<script>
var myWindow;
function showNews()
{
myWindow = window.open("","myWindow","width=200,height=100");
myWindow.document.write("<p>This is 'myWindow'</p>");
//tmot = setTimeout(function(){myWindow.open('http://www.yahoo.','_self')}, 2000);
tmot = setTimeout(function(){myWindow.close();}, 5000);
}
</script>
DEMO:
View demo
But if we unment the line where we load yahoo., it doesn't work anymore:
View demo
What to do?
You could try using a hack, something like this:
HTML:
<button type="button" id="show-news">Show News</button>
jQuery:
$("#show-news").click(function() {
myWindow = window.open('','myWindow', "width=200, height=200");
setTimeout(function() {
myWindow.window.open("http://www.w3schools./", "_self");
}, 2000);
setTimeout(function() {
myWindow.window.open('', '_self', '');
myWindow.window.close();
}, 5000);
});
I got a solution after a whole day trying the same thing. My window.open occured in some jQuery success result and I thougt first it was because I could not propagate the myWindow var from "success" result to main DOM.
In fact it's only that javascript does not accept to manipulate any window once a new content has been loaded (I mean URL). But it can be hacked still, since it seems javascript ONLY refuse manipulation if there is a NEW content. So if you reload your window with exactly the same content, then it suddendly bees manipulable.
So somewhere in my code (even in a function) I open the window (wherever it is), with no need to set it in var.
window.open('myURL','WindowName', "width=200, height=200");
Then wen I decide to close the window (wether it is a SetTimeOut or any trigger), I just have first to reload the window with exactly the same URL and same name , (and this time with a var set). And now I can magically close it.
var windo = window.open('myURL','WindowName', "width=200, height=200");
windo.close();
Don't ask me why it works. It does.
(I guess it's because it's not a new window not new content for javascript, so rules are respected.)
I tested this in JSFiddle on Chrome.
The timeout is, in fact, firing. The problem is that certain browsers do not allow you to close a window using javascript.There are bugs that prevent it - specifically that the browser won't let code from one website close a window that is opened to a different website. See the ment below for a better explanation.
When I tried this in Chrome the setTimeout method fired, but the window did not close. I tested it by calling the 'alert' method rather than the news.window.close() method.
本文标签:
版权声明:本文标题:javascript - Using setTimeout with window.close() not work after using setTimeout with window.open() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741246637a2365001.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论