Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need: "Are you sure you want to close this window?"

Status
Not open for further replies.

sbayter

IS-IT--Management
Nov 14, 2002
95
CO
Hi,
I need a script that when the user tries to close the browser it ask "Are you sure you want to close this window?"
I tried to write but when the user answers not it closes anyway.
This is what i have:

<script>
function close(){
var close=confirm(&quot;Are you sure you want to close this window?&quot;);
if (close)
return True;
else
return False;
}

Thanks in advance,


sbayter
 
<script>
function myclose(){
if (confirm(&quot;Are you sure you want to close this window?&quot;)) window.close();
}
</script>
<body>
<a href=&quot;javascript: myclose();&quot;>Close</a>
</body>


close is a reserved word try not to use that as the name of your function and your variable.
This will act differently in IE and NN.



bungee.gif
make an impact
 
just a slight modification to your script:

***
<script>
function closeoption(){
var close=confirm(&quot;Are you sure you want to close this window?&quot;);
if (close)
return true;
else
return false;
}

</script>
<input type=button value=&quot;close window&quot; onclick=closeoption();>
***

what should it do next?

- g
 
you might have seen this in IE using the onbeforeunload event handler.

<body onbeforeunload=&quot;return 'Are you sure?';&quot;>

=========================================================
while (!succeed) try();
-jeff
 
Here's a silly idea: Maintain the &quot;Are you sure you want to close this window?&quot; window under your current window. It'll have lots of invisible controls and basically keep track of the status of your session. If they close the window for a legitimate reason, you can just surreptitiously close the &quot;tracking&quot; window first. Otherwise, if they just try to bang out, they're faced with the pleasantly intimidating &quot;Are you sure you want to do this?&quot; window. Now, on that window, you'll have a &quot;Yeah, get me the heck outta here.&quot; button, which will close the baby window and not do anything. But, if they hit the &quot;Holy smokes, I didn't mean to lose stuff!&quot; button, then that li'l window uses the data in the hidden fields (which has been kept up-to-date, right, and includes all variables, including window size and location?) to re-create the old window.

Not all browsers are as sloppy about onunload events as IE, and this will handle those more discriminating browsers, such as Mozilla or Netscape. Even if they bang out by hitting the &quot;X&quot; button, they'll still be prompted for a final exit. The only way a user might override this is to go into their list of processes and kill all occurences of their browser. And I assure you, that is one power you do not want to mess with.

Cheers,


[monkey] Edward [monkey]

Like Lovecraft? Know Photoshop? Got time for the Unspeakable?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top