Forum Moderators: open

Message Too Old, No Replies

Close window button

Close window button

         

chapteryx

2:44 pm on Sep 30, 2005 (gmt 0)

10+ Year Member



I can't believe I'm asking this, but I can't see the error, or I'm having a brain cramp or something.

After sending an email message, a new window shows saying thank you and provides a button to close. Easy, eh?

Well, what's wrong with the code below, and why don't either of the buttons actually close the window?
I tried self.close, parent.window.close, top.window.close : nothing seems to fire.
Using Firefox to test.

Thanks for helping this brain-cramped old man! :-)

<html>
<head>
</head>
<body>
<table width="370px" align="left">
<tr><td>
<h3 align="center">Thank You for your interest in our services.</h3>
<p align="center">We will contact you via your preferred method within three business days</p>
</td></tr>
<tr><td>
<p>&nbsp;</p>
</td></tr>
<tr><td>
<p align="center"><input type="button" onClick="javascript:window.close();" value="Close Window"></p>
</td></tr>
<tr><td>
<form method="post">
<input type="button" value="Close Window" onClick="window.close();">
</form>
</td></tr>
</table>
</body>
</html>

kaled

3:36 pm on Sep 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the first example, the "javascript:" part should be deleted for an onclick event.

In Firefox (tools¦javascript console) I get the following error message:-
"Scripts may not close windows that were not opened by script."

Kaled.

chapteryx

3:42 pm on Sep 30, 2005 (gmt 0)

10+ Year Member



Thanks.

But javascript or no javascript, the button still does not close the window.
So, maybe I should ask: how do you create a button that closes the current window?
All examples I have seen fail to operate, and I don't get it :-)

rocknbil

4:06 pm on Sep 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Basically kaled answered your Q, but let me reword it: if you as a user open a browser instance, the browser won't allow a scripted method to close that window, at least not without a warning. You **should** be getting a message from I.E., something to the effect of "Another user is attempting to close this window, do you want to allow it to close?"

But if you use some scripting method to open a new window

var win=open('test.html','123456','width=400,height=500,scrollbars');

Then window.close() will work to close test.html. if you directly open test.html, it will warn you, or not close it at all.

Imagine if this security feature were not in place: you visit a site, and they close all your referring windows, pop up all their ads and effectively suck your entire computer into an internet black hole. :-)

chapteryx

4:33 pm on Sep 30, 2005 (gmt 0)

10+ Year Member



I think I understand the answers that both kaled and rocknbil offer, and am very grateful.
I do understand about what the security feature provides, and that a window that is not opened by a script should not be closable by a script without a warning.

But there's maybe something I'm not being clear about.

The window I provided as example is not attempting to close any other window, and does not automatically try to even close itself.

I am suggesting that it should be possible for a user to click on the button on an open window, and have that same window close *just like they can if they click on the charming little X in the top right corner*.

There is certainly no security issue here at all, as the operation being performed is no different from operations that are available to the user normally.

20 years of programming suggest to my addled brain that this is not asking very much.

So are people trying to tell me that HTML does not permit such a button, which is manually fired by the user?

I'm not trying to be rude or obtuse, but what unscripted method can I use to effect this operation?

kaled

5:52 pm on Sep 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Creating an alternative "close" button seems reasonable BUT, in your case, you could make that button/link say something other than "close".

The only secure way to allow this would be for html to define a "close" button, the caption of which was set by the browser not the webmaster.

Kaled.