Forum Moderators: open

Message Too Old, No Replies

Window close with set timeout

         

Joe Belmaati

7:11 pm on May 27, 2008 (gmt 0)

10+ Year Member



Hello,
I am struggling getting a window to close using the setTimeout function. In simple terms this is what I am working with

<input type="submit" name="mywar" value="Click" onclick="setTimeout('window.close()', 500)" />

I have tried window.close, window.self.close, self.close, and _windowname.close

but neither works. Please, if someone could let me know what I am doing wrong I'd be sincelely greatful.

Best,
Joe

[edited by: Joe_Belmaati at 7:12 pm (utc) on May 27, 2008]

rocknbil

2:14 am on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can only assume you're trying to close the main window, and you can't do that. It won't let you. In FireFox, in some cases, you will get some error to the effect of "denied" or a pop up warning you it's trying. I'm guessing it's because you have a setTimeout that's not giving you the message. Your code should work fine on any window you open.

Working example, the escapes \ are only because I did a document.write() to my new window. Set the timeout to 2000 to verify.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- doctype all on one line! -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Set Timeout</title>
<script type="text/javascript">
function newWin() {
var day = new Date();
var id=day.getTime();
var params = 'width=400,height=400,scrollbars,resizable';
var msg='<p><a href="#" onClick="setTimeout(\'window.close()\',500);return false;">'+
'This window will self-destruct half a second after you click this link</a></p>'+
'<form><input type="button" onClick="setTimeout(\'window.close()\',500);"'+
' value="Or click this button"></form>';
var win=open('',id,params);
win.document.write(msg);
win.document.close();
return false;
}
</script>
</head>
<body>
<p><a href="#" onClick="return newWin();">open new window</a></p>
</body>
</html>

Joe Belmaati

7:23 am on May 28, 2008 (gmt 0)

10+ Year Member



Thank you so much for your reply. Actually the window I am trying to close is a child pop up launched by the main page. It's a sort of "edit this entry" pop up window which gives a message on success, and then I want it to close automatically efter half a second. I'll see if I can work around your code to get it working. Thanks a lot :-)

daveVk

8:15 am on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you open the pop up you will need to save its handle in global var

popWindow = open( ...

Use this name in setTimeout.

setTimeout('popWindow.close()', 500)

Joe Belmaati

8:30 am on May 28, 2008 (gmt 0)

10+ Year Member



Thank you very much. The window opens with

onclick="window.open('airport.php?mode=edit&id=20&aid=1', '_editwindow', 'resizable=0,width=800,height=440,top=200,left=200'); return false;">

...still doesn't work with _editwindow.close()

daveVk

12:21 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try

onclick="popWindow=window.open('airport.php?mode=edit&id=20&aid=1', '_editwindow', 'resizable=0,width=800,height=440,top=200,left=200'); return false;">

and

popWindow.close();

outside of all functions should declare

var popWindow;

Joe Belmaati

12:43 pm on May 28, 2008 (gmt 0)

10+ Year Member



Thank you once again. I am now working with

onclick="setTimeout('popWindow.close()',500);"

having changed the launch code to

onclick="popWindow=window.open('airport.php?mode=edit&id=20&aid=1', '_editwindow', 'resizable=0,width=800,height=440,top=200,left=200'); return false;">

...but for some reason the window still does not want to close. ... :-(

"outside of all functions should declare

var popWindow;"

Sorry to sound like a rookie, but how do I do that? Thank you so much for your help.

[edited by: Joe_Belmaati at 12:50 pm (utc) on May 28, 2008]

rocknbil

4:21 pm on May 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you open the pop up you will need to save its handle in global var

This shouldn't be necessary, see my previous example. window.close() will reference the current window in any case, which is the current popup window. This is why I thought you were trying to close the "opener" window.

Perhaps a full code snippet, as I did in my working example, would be helpful.

[edit] AH. I just tried this in Internet Exploder, and locally, I'm getting the "Internet Explorer has restricted this page" warning. If you have that disabled or are ignoring it, try your server version?

Joe Belmaati

10:26 pm on May 28, 2008 (gmt 0)

10+ Year Member



Sorry to keep moaning but I can't get any of these to work. Any other suggestions...?

daveVk

1:08 am on May 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure I understand context of what you are trying. 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 ?

If you wish to open the popup and unconditionly close it after a period then try in main page

<script>
var popWindow;
</script>
...
<body>
...
onclick="popWindow=window.open('airport.php?mode=edit&id=20&aid=1', '_editwindow', 'resizable=0,width=800,height=440,top=200,left=200'); setTimeout('popWindow.close()',500); return false;">
...
</body>

Joe Belmaati

2:28 pm on May 29, 2008 (gmt 0)

10+ Year Member



Hi again,
yes, I am trying to close the pop window using code in the pop window itself. As long as I don't use setTimeout it works, but it also closes the window immediately. As soon as I use setTimeout the window never closes...

daveVk

1:46 am on May 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case ignore my prior posts, can not see why window.close() does not work in this context. Any script error indication ? Have you tried other browsers ?

Joe Belmaati

8:05 am on May 30, 2008 (gmt 0)

10+ Year Member



Hello again,
I do not get any errors and I have tried with

Win XP IE6, FF2
Mac OS X Safari 3, FF2

daveVk

1:22 pm on May 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try giving task of closing pop up to parent window

replace

setTimeout('window.close()', 500);

with

window.opener.popWindow=window;window.opener.setTimeout( "window.popWindow.close();",500);

Joe Belmaati

9:08 pm on May 30, 2008 (gmt 0)

10+ Year Member



Thank you so much for the suggestion, and thank you for keeping suggesting things to try. Unfortunately that snip did not work either. I'm at a loss here...

daveVk

6:48 am on May 31, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<input type="submit" name="mywar" value="Click" onclick="setTimeout('window.close()', 500)" />

Looking back at your original post, I see you are doing this on the submit button, when the timeout occurs the submit reply may or may not have completed.

Consider putting the window close logic in the submit reply page.

Joe Belmaati

8:28 am on May 31, 2008 (gmt 0)

10+ Year Member



Thank you for suggesting that. It lead me in the right direction. I put a spacer.gif on the submit reply page and put the call to settimeout into the onload event inside the img tag, and that worked. Seems a goofy and "unclean" solution though. This was really doing my head in!

Once again thank you so much for your help. I sincerely appreciate it :-)

[edited by: Joe_Belmaati at 8:29 am (utc) on May 31, 2008]