Reputation: 51
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.com','_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!
Upvotes: 5
Views: 16508
Reputation: 152
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 becomes 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.)
Upvotes: 1
Reputation: 12300
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.com','_self')}, 2000);
tmot = setTimeout(function(){myWindow.close();}, 5000);
}
</script>
DEMO:
But if we uncomment the line where we load yahoo.com, it doesn't work anymore:
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.com/", "_self");
}, 2000);
setTimeout(function() {
myWindow.window.open('', '_self', '');
myWindow.window.close();
}, 5000);
});
Upvotes: 1
Reputation: 361
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 comment 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.
Upvotes: 0
Reputation: 1571
You'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.com', "NewsWindow")},2000);
tmot = setTimeout(function(){news.close()}, 5000);
}
Not try on IE, i don't have it.
Upvotes: 3