PDA

View Full Version : How To Make A Pop-Up Window Automatically Close After A Certain Amount of Seconds?



Dankees
11-08-2013, 11:52 PM
Any idea how I can make a pop-up screen completely close after a certain amount of seconds? Thank you.

djr33
11-09-2013, 12:24 AM
You can look up window.close() and use a timer to make a delay.
But this also depends on how you are creating the popup. If you are making the popup yourself (which you most likely are), then this is easy. But you can't do it unless it was opened as a popup in this way. It's a little complicated, so:
Please post the code you're currently using or give us a link to your page.

Dankees
11-09-2013, 01:26 AM
Thanks. I will look. It is a page that opens after a web form submission. It is at: http://www.FinancialAidFiler.com/Form/ThankYou.html

djr33
11-09-2013, 01:27 AM
Is that the page that opens the popup or the page that is opened as a popup?

Dankees
11-09-2013, 02:59 AM
Well, a page is popped up from the main site with a web form. Then, upon submission, the thank you page then opens where the web form once was.

If you'd like, you can see what I mean by going to http://www.FinancialAidFiler.com/ContactUs.html and clicking the "Contact Us For More Information" button.

Thank you.

djr33
11-09-2013, 03:41 AM
That doesn't guarantee that everyone always gets to that page via the popup link (in which case closing it with Javascript might not work-- it might be blocked by the browser). But in general, almost everyone will probably get there that way, so you should be able to close it.

So you should be able to do what I suggested in my earlier post:
1. Use window.close() to close the window.
2. Use a timer to delay that action, with setTimeout().

A quick search for how to use those should give you enough information to put something together :)

Dankees
11-09-2013, 03:55 AM
I'm pretty new at this - is there a script somewhere that you recommend I use? Thank you.

djr33
11-09-2013, 04:44 AM
This is really not very hard to do. You don't need a "script", just a few lines of code. (It's so simple you won't find anything exactly like this, except perhaps in a tutorial/explanation, and you can easily make it yourself.) It's fine that you're just starting, but at some point you'll have to just figure things out, and this is a good case-- it won't be too hard.
For the first part:
https://www.google.com/search?q=close+window+javascript
For the second part:
https://www.google.com/search?q=timeout+javascript

The other trick you'll need is to activate this when the page loads. There are a few ways to do that, but the simplest is just something like:
<body ... onload="myFunction()">
Then define "myFunction" above in the head section. You could even just put all of the code (it'll be short) as a line in the body tag's onload attribute.


The idea of this forum is to offer help and advice, not to do projects for you. So in this case you can start off with a fairly small task and see if you can get it to work. If not, feel free to post here again. (As I said, there could be some complications, but my guess is that they won't come up given the configuration you're using.)

molendijk
11-09-2013, 03:32 PM
Try this. In this example, the DD page opens in a (centered) new window that takes up 70% of the screen's width and 80% of the screen's height. De popup-window closes after 10 seconds:

<head>
<script>

function windowsize()
{
window_height=Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight)
window_width=Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth)
w=window_width/(100/width), h=window_height/(100/height);
l = (window_width-w)/2-10;
t = 15+(window_height-h)/2;
}

function size_it(the_width,the_height)
{
width=the_width;
height=the_height;
windowsize();
}


function pop_it_up(url,the_width,the_height)
{
size_it(the_width,the_height);
pop_it_up_prep(url)
}

var popUpWin ;
var url;
var autoclose = true;

function pop_it_up_prep(url){
if (popUpWin) {popUpWin.close()}
if (popUpWin && autoclose){window.onunload=function(){popUpWin.close()}}
popUpWin=window.open(url,"","menubar=no, toolbar=yes, titlebar=no, directories=no, status=0, scrollbars=yes,location=yes,top="+t+",left="+l+",width="+w+",height="+h+", resizable=1");
if (typeof(popUpWin)=='undefined') {
alert("Your browser tries to open:\n\n"+unescape(url)+".\n\nThat's a safe page, but your safety settings prevent to open it. Please disable your pop-up blocker and click the link again.");
}
if (window.focus){popUpWin.focus() }

}
</script>
</head>

<body>
<a href="javascript: void(0)" onclick="pop_it_up('http://www.dynamicdrive.com', '70', '80'); setTimeout('popUpWin.close()',10000); return false">DynamicDrive</a>
</body>