Log in

View Full Version : close window automatically



s00263668
08-01-2006, 02:44 AM
In my design, i want my Internet Explorer close automatically. Here is my code:


<script LANGUAGE="JavaScript">
function closePg(){
window.close();
return true;
}
</script>
<body onLoad="return closePg()"></body>

However, a pop up message was displayed after that. If nobody click the OK button, then the page will not close itself.
445

Twey
08-01-2006, 02:55 AM
Yup. That will happen.

s00263668
08-01-2006, 03:57 AM
Yup. That will happen.

anyway to avoid that message box from pop up?

jscheuer1
08-01-2006, 04:15 AM
Only works in some browsers but, IE (the one that generated your confirm dialogue box) is one of them:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function closeIt(){
window.opener=self;
window.close();
}
</script>
</head>
<body onload="closeIt();">

</body>
</html>

s00263668
08-01-2006, 05:16 AM
Yeah, this is the thing i want!
thank you guys.

mburt
08-01-2006, 03:52 PM
Yes, but is it?? -*Insert maniacle laughter here

This is my personal favorite way to do this.


<script language="javascript">
onload=function closeIt(){
var cl=window.confirm("Close the window?")
if (cl) {window.opener=self;window.close()}
else {return closeIt()}
}
</script>

This does tend to tick off users

edit: don't use this, jscheuer1 is going to try to ban me for it!

Twey
08-01-2006, 04:22 PM
Isn't that rather what the OP was hoping to avoid in the first place? :-\

shachi
08-01-2006, 04:24 PM
Yea I think so too.

mburt
08-01-2006, 04:28 PM
But, it forces you to close the window. When you hit "cancel" it returns the function.

Twey
08-01-2006, 04:47 PM
Argh! I just read that. That's *awful.*

Recursion is not a good idea, especially when you don't control the level to which it will recurse :) Every time the user clicks "Cancel," the function is run again, but the original call will still be in memory, since it won't return until the child does. That means that by the time they've clicked "Cancel" twenty times, you've got twenty-one copies of the function running, all at once.

Of course, if the user panics and just holds down enter, you could end up with 200+ copies of this function running -- a sure way to bring a monolithic system to its knees.

Whatever you do, do not use that code. :)

mburt
08-01-2006, 04:48 PM
I know, I'm horrible.. :)

jscheuer1
08-01-2006, 05:02 PM
Interestingly enough, in FF there would be no dialogue for the window.close() call itself but, FF just ignores what it considers illegal and is not fooled by the opener=self trick. But FF will probably still pop up the hard coded confirm box. What a mess!

Can't we ban mburt for posting malicious code?

mburt
08-01-2006, 05:03 PM
NO!!!! I HAVE BEEN DEFEATED....... Just joking.. Don't worry, I won't do anything stupid like that again :)

Twey
08-01-2006, 05:29 PM
Heh... I think the banning point should come when he actually uses it. :p

mburt
08-01-2006, 05:31 PM
I'm totally with you on this one. :)

s00263668
08-03-2006, 05:43 PM
sorry i just finish my project and haven't been here for few days.

what have mburt post? Honestly i don't understand at all.

(I'm really "noob" in programming)

jscheuer1
08-03-2006, 08:41 PM
sorry i just finish my project and haven't been here for few days.

what have mburt post? Honestly i don't understand at all.

(I'm really "noob" in programming)

All you need to know is, don't use his code from this thread.

I was kidding about having him banned but, hopefully this will make him a bit more careful about publishing substandard code. Think first, post later, or better yet, think again, try out a demo in a couple of browsers, tweak things, think again, test again and post even later.

Twey
08-03-2006, 08:43 PM
Ignore his code. Pretend it was never posted. :)

mburt
08-04-2006, 12:49 AM
...Yeah... what he said..

jscheuer1
08-04-2006, 03:29 AM
I concur.