Javascript: using setTimeout with window.close() not work after using setTimeout with window.open()

window.open not working
settimeout javascript not working
javascript close window after 5 seconds
let timeoutid settimeout function console log yes 3000 cleartimeout timeoutid
window.open javascript
window close not working
javascript popup window onclick
window.open new tab

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!

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.

Window setTimeout() Method, The clearTimeout() method clears a timer set with the setTimeout() method. myVar = setTimeout("javascript function", milliseconds);. Then, if the function has not already been executed, you will be able to stop the execution by The numbers in the table specify the first browser version that fully supports the method. The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds. Tip: 1000 ms = 1 second. Tip: The function is only executed once. If you need to repeat execution, use the setInterval() method. Tip: Use the clearTimeout() method to prevent the function from running.

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.)

Window clearTimeout() Method, Learn how the JavaScript function setTimeout() works, how it can be used with If you don't see the popup open, please visit CodePen and run the demo there. For the purposes of this tutorial, I'll omit window , but ultimately, which This won' t work when using an arrow function in the introduce method,� If you are trying to close the popup using some code within the popup then follow Joe's directions, window.close() should be Ok. I was assuming you were trying to close the popup from the main window, you mention two onclicks, the one that opens the pop up is assumably within the main page, where is the other ?

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.

JavaScript setTimeout() Function Examples, So, popups is not something we use everyday. What if the popup opens from onclick , but after setTimeout ? That's a bit tricky. If we decrease the delay, the popup works in Firefox too: To close a window: win.close() . Only if you open a new window using window.open() will the new window be able to close using code as I have mentioned above. This works perfectly for me :) Note : Never use href to open the page in a new tab. Window.close() does not work with "href" . Use window.open() instead.

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:

View demo

But if we uncomment the line where we load yahoo.com, 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.com/", "_self");
    }, 2000);

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

});

Popups and window methods, open after 3 seconds setTimeout ( ( ) => window . open ( 'http://google.com' ) , 3000 ) ; If we decrease the delay, the popup works in Firefox too: A JavaScript in it may use window.opener to access the window that opened it. but window.close() is ignored by most browsers if window is not created with window.open() . After the call to setTimeout() the script continues normally, with the timer running in the background. In the above simple example we embedded the entire code for our JavaScript alert box in the setTimeout() call. More usually, you’d call a function instead.

Popups and window methods, Actual results: setTimeout does not works with delay Expected results: it should In this ticket, it seems that there are some limits on eval calling but I am not using it. (b) While alert() by myfunc() at sub window is kept open, and while alert() by because composition window is not cached and actually closed from Tb 17)� Then I can close this window, using. window.close(); But to close the window which is not opened by window.open(), just do one thing. open any other URL or a blank page in the same tab. Then close this newly opened tab (because it is opened by window.open()) code:- <script> window.open("", "_self"); window.close(); </script>

814651, Passing a string instead of a function to setTimeout() has the same associated problems as using eval. // Recommended window.setTimeout(� window.close(); Examples Closing a window opened with window.open() This example shows a method which opens a window and a second one which closes the window; this demonstrates how to use Window.close() to close a window opened by calling window.open().

WindowOrWorkerGlobalScope.setTimeout(), The window.location object can be used to get information on the the function until clearInterval() is called, or the window is closed. To execute a function only once, after a specified number of milliseconds, use the setTimeout() method The setTimeout() method sets a timer in milliseconds, then calls a� The difference is that Firefox treats a timeout of 2000ms or less are acceptable, but after it – removes the “trust”, assuming that now it’s “outside of the user action”. So the first one is blocked, and the second one is not. window.open. The syntax to open a popup is: window.open(url, name, params): url An URL to load into the new

Comments
  • I tried this in Firefox (27) and IE (11), and it works fine.
  • Note that you lose your reference to the first timeout when you set the return of the second to tmot.
  • I tried this in chrome here: jsfiddle.net/u6EaX And it did not work. Once the window opens a new site the original window can no longer control it, at least on Chrome.
  • Thank you! That was the trick! (removing "news." from the setTimeout lines.) Now to figure out why that is the case...
  • Just figured out why. Thank you!
  • I don't think it's a bug. Once the window is loaded with content from a different domain, the browser probably considers it to be off limits.
  • You're right - I shouldn't have said 'bug'. Sometimes I use the term 'but' when I really mean "the code didn't do what I wanted it to do, but rather what it was designed to do". I'm editing the answer.