Click to See Complete Forum and Search --> : alert user that window is closing
jazzyjade
06-02-2003, 11:47 AM
I have a window which I opened with Javascript. If a user clicks the X button to close the window, I'd like a vbscript to run which would warn the user that the page is closing and give them the choice to cancel that action.
How do I trigger the function to run?
Thanks!
Khalid Ali
06-02-2003, 11:58 AM
you can use something like this
window.onunload = function(){
alert("Window is beiong closed")
}
just outta curioisity...
Don't ya think when a window is closed theuser will actuall notice that?..;)
You could do this after the user closes the window, however, it will have to reopen. I'm not sure about VBScript, but in JavaScript there is a onBeforeUnload (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jslrfjscriptlanguagereference.asp) event that you might want to try to use.
Jona
fla5hba5h
06-02-2003, 11:59 AM
You don't need VBscript. You can open one with Javascript. I think this will work:
(inside the code)
confirm("Would you like to close this window?");
self.close();
onBeforeUnload is IE only proprietary code, I am nearly certain...
Yes, but he was originally willing to use VBScript, which is also IE-only, so unless he didn't know that VBScript worked only in IE (and I think he did), then he should have no problem using onBeforeUnload.
Jona
Here is the DevEdge (Netscape) object reference: http://devedge.netscape.com/library/xref/2002/client-data/property-data-window.html
Jona
Khalid Ali
06-02-2003, 12:21 PM
Originally posted by pyro
onBeforeUnload is IE only proprietary code, I am nearly certain...
You are right Pyro,
It is IE specific..method...good one jona...:D
This is what Microsoft says for every IE specific item
Standards Information
There is no public standard that applies to this event.
In other words its only for MS specific tools:D
Originally posted by Khalid Ali
This is what Microsoft says for every IE specific item
Standards Information
There is no public standard that applies to this event.
In other words its only for MS specific tools:Dlol... :D
jazzyjade
06-02-2003, 12:34 PM
I'm on IE 5.0+ and working on an intranet. I'm choosing to warn the user because it's the final page of the exam and the exam needs to print before the user should close the window. So I'd like the user to ensure that the exam printed by giving this warning: (go pick up exam from printer before closing window).
I wanted to use vbscript so I could control the alert box with an "ok" and "cancel" button, which I thought was unavailable with just Javascript.
I tried the beforeUnLoad event but the window still closed when the script ran. I want something that prevents the window from closing when the X is clicked unless the user chooses the OK button (as opposed to Cancel) from the VBscripted alert window.
scriptkid
06-02-2003, 12:37 PM
if(confirm(string)){ continue; } else { break; }
confirm returns a boolean value
true if the user clicks okay and false if the user clicks cancel
jazzyjade
06-02-2003, 12:40 PM
Originally posted by scriptkid
if(confirm(string)){ continue; } else { break; }
confirm returns a boolean value
true if the user clicks okay and false if the user clicks cancel
:confused: I'm sorry...I'm not that familiar with scripting that this makes much sense. I get the structure but I'm still not sure how this would prevent the window from being closed witht the X button. Thanks!
Khalid Ali
06-02-2003, 12:41 PM
I think this might help
var answer=" "
var question = "Do you really want \nto close the window.?";
var answer = confirm(question);
if ( answer ){//yes
return true;
}else if(!answer){
return false;
}
If you went to the link I provided for the onBeforeUnload event, you'll see an example on the Microsoft Web site. Learn from the example given. It is a very simple task from there.
Jona
scriptkid
06-02-2003, 01:10 PM
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events/onbeforeunload.asp
<--Thats the link jona was refering to
jazzyjade
06-02-2003, 01:20 PM
Originally posted by scriptkid
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events/onbeforeunload.asp
<--Thats the link jona was refering to
Thanks! I went to the original link and was having the damnest time finding this.
My bad, I accidentally posted the wrong link. :p
My apologies! But the link scriptkid posted was the right link. ;)
Jona
jazzyjade
06-02-2003, 02:03 PM
I tried this code:
function checkClose(){
var answer=""
var question = "Do you really want to close the window?";
var answer = confirm(question);
if (answer) { //yes
return true; }
else if (!answer){
return false;}
}
<body onBeforeUnload="checkClose()">
And whether choosing Ok or Cancel, the window closes.
All right, try this instead. Sometimes without the comparison, the statements return true either way.
function checkClose(){
var answer="";
var question = "Do you really want to close the window?";
var answer = confirm(question);
if(answer==true){return true;}
else{return false;}
}
Jona
jazzyjade
06-02-2003, 02:15 PM
Sorry, but the same thing still happens. :(
function checkClose(){
var question = "Do you really want to close the window?";
if(!confirm(question)){return false;}
return true;
}
Jona
jazzyjade
06-02-2003, 02:25 PM
Oh boy. Same thing again! I don't suppose you have any other tricks up your sleeve? I'm not sure why this won't work (or my original vb script) but it's always the same thing--the alert runs and either option the user chooses causes the window to close. It's like there's no way of stopping it--it has a mind of its own. :)
scriptkid
06-02-2003, 02:30 PM
post all the code plz...i have a feeling that theres something important that hasnt been posted here
AdamGundry
06-02-2003, 02:30 PM
It's probably a browser security feature - if Javascript could prevent a window from closing, you could get a malicious site which would not go away!
I believe you can call window.open() in the event handler though, which might help.
Adam
It is, as Adam suggested, a security issue. Try this instead:
<script>
function checkClose(){
var q = "Do you really want to close the window?";
if(confirm(q)){return true}
else {window.open(location.href);}
}
</script><body onbeforeunload="checkClose();">
Jona
jazzyjade
06-02-2003, 02:40 PM
So, you did have one last trick, Jona! Thanks much.
Well, that works, but now I have another problem. The page now opens in a new window (separate from the frameset that I originally had it in) and none of the dhtml variable content from previous pages writes to the page.
Hm... How did you have the original page get the DHTML variables and all that other stuff?
Jona
This works in IE5+
<script language="javascript">
<!--
function quit(){
event.returnValue="Are you sure you have finished?"
}
// add "onbeforeunload=quit()" to the opening body tag
// -->
</script>
<body onbeforeunload="quit()">
Mr. J, I didn't even think about that! I can't believe that. :o All right, so you win. :)
Jona
diamonds
06-02-2003, 02:59 PM
I don't use onbeforeunload
In fact, I didn't know there even was an onbeforeunload.
I use onunload.
jazzyjade
06-02-2003, 03:00 PM
Thanks a million! That little code works perfectly.
I'm new to this site but I would like to thank everyone for their help. Unlike most sites, the help I received here pointed me to documentation so that I could learn/understand the code and not only was everyone polite and went beyond what I expected, but the responses were very timely. I don't know if I should tell everyone I know about this great site or keep the secret to myself. :D
Tell everyone about this site! :p
diamonds, if you read all of the replies you'll see that onBeforeUnload is IE-only, and you'll see a link to it and stuff like that.
Jona
jazzyjade
06-02-2003, 03:05 PM
Oh crap. Here I thought I'd leave a nice compliment and leave you poor fellas (and gals) alone, but nope, I'm back already.
I just realized that the unbeforeunload script affects another function on my page. Is there a way I can disable the quit() function inside another function so that it doesn't fire?
I have a button that when clicked, writes an entirely new page with more variable content. When that button is clicked, I don't want quit() to run. Any way to get this to work? I never had to "cancel" a function before.
Because the event is onBeforeUnload, you cannot do it. If your event was different, you could set a global, boolean variable that if true would run the function, if false would return false and not run the function... It looks like you might not be able to do that because of the event... But I have helped, "iceafreak99" with a script that is similar to this at http://goldreject.com/. This event is onUnload, of the form in the top window when you click the "green light" at the bottom right-hand side of the main page.
Jona
Originally posted by Jona
Because the event is onBeforeUnload, you cannot do it.Sure you can... in the quit button, just pass a value to the function... try something like this:
<script language="javascript">
function quit(what){
if (what == "prompt") {
event.returnValue="Are you sure you have finished?"
}
else {
window.close();
}
}
</script>
<body onbeforeunload="quit('prompt')">
<a href="#" onclick="quit('noprompt'); return false;">Close</a>
NOTE: this will still prompt the user, as it is a secruty issue to close a non-javascript opened window with javascript...
Aww, man! I didn't really think about that one. I knew there was something to it, but I was confused.. And I'm under pressure right now. I have to go! :p
Thanks, for clearin' me up, though.
Jona
But I don't think he said he was closing window just writing to it?
Then just clear the else...
jazzyjade
06-02-2003, 03:47 PM
That looks like it would work. You're right that I'm not closing the window, only writing a new page to it, but I could just place my newPage() functions in place of the window.close.
I'll try it out and report back on how it works. Thanks!
jazzyjade
06-03-2003, 10:40 AM
I guess this didn't work. Whenever quit is run, even from the <a> tag, the what variable returns "prompt" so the body tag must be overriding it.
<script language="javascript">
function quit(what){
alert(what);
if (what == "prompt") {
event.returnValue="Are you sure you have finished?"
}
else {
alert('Working');
}
}
</script>
<body onbeforeunload="quit('prompt')">
<a href="#" onclick="quit('noprompt'); return false;">Close</a>
Try replacing, "alert('Working');" with this:
document.body.onbeforeunload="";
Jona
jazzyjade
06-03-2003, 11:09 AM
That doesn't do it either--it still returns 'prompt' before the If statement runs. I'm pretty sure that when quit() is run from the <a> tag it, it also runs from the body tag and the body tag overrides it so that "prompt" is always returned so the quit() function never makes it to the else statement.
##
I got it to work! I set a global variable called printexam and set it to 0. Then I wrote a function that when the <a> tag is clicked sets printexam to 1. I then changed the quit() function to check if printexam equals 0 and if it does then the alert pops up and if it doesn't equal 0 it writes my new page without the alert prompt.
Thank you again to everyone who helped me on this! :p
sencinias
06-20-2003, 08:10 AM
I have a data entry form created for users, I need a way to stop them if they try to close the browser by clicking the X or by using File/Close. If they perform one of those, I want to prompt them and then if they choose to continue, close else remain on the page. This form submits to itself though so when they click the "Save" button, I don't want them to get prompted. Is this possible? Below is a piece of my code.
<SCRIPT language="JavaScript">
function quit(what){
alert(what);
if (what == "prompt") {
event.returnValue="Are you sure you have finished?"
}
if (what == "noprompt"){
document.form.submit();
}
//else {
//window.close();
//}
}
</SCRIPT>
<BODY onunload="quit('prompt')">
<INPUT type="Submit" name="Submit" value=" Save " onclick="quit('noprompt');">
</BODY>
I'm getting more familar with JavaScript but still have a lot to learn, any help is appreciated. Thank you.
sencinias
06-20-2003, 09:16 AM
You can disregard my last question, I missed the update that jazzyjade had to fix the problem and I've used the same idea and now my pages work as well.
Thank you.