View Full Version : Catching attempts to close the browser
matthewjumps
September 6th, 2007, 02:08 AM
http://www.customink.com/lab/ features an extensive javascript/ajax tshirt design utility, but one of the things im interested in is the function that is triggered when the user attempts to navigate away from/close the page if they have made any changes. an alert is displayed that warns the user that they will lose their edits, preventing the page from being closed.
how is this achieved? any ideas? im assuming its done using javascript? any help would be appreciated :azn:
MTsoul
September 6th, 2007, 01:04 PM
I closed it just fine...
ratherblue
September 6th, 2007, 03:33 PM
I closed it just fine...
In what browser? Firefox shows the popup for me
MTsoul
September 6th, 2007, 05:05 PM
In Opera.
Surrogate
September 6th, 2007, 06:16 PM
http://www.customink.com/lab/ features an extensive javascript/ajax tshirt design utility, but one of the things im interested in is the function that is triggered when the user attempts to navigate away from/close the page if they have made any changes. an alert is displayed that warns the user that they will lose their edits, preventing the page from being closed.
how is this achieved? any ideas? im assuming its done using javascript? any help would be appreciated :azn:
http://www.w3schools.com/jsref/jsref_onunload.asp
matthewjumps
September 6th, 2007, 09:52 PM
thanks Surrogate thats great - just one question, the onunload event detailed on the w3schools tutorial shows an alert, but the window is closed regardless...is there a way to make the window not close unless the user clicks ok?
matthewjumps
September 11th, 2007, 01:17 AM
*bumps* does anyone have any thoughts?
monitoiul
September 11th, 2007, 01:44 PM
try reading this (http://en.allexperts.com/q/Javascript-1520/Prevent-Closing-Page.htm)
ps: the code is as follows:
var needToConfirm = false;
function setDirtyFlag()
{
needToConfirm = true; //Call this function if some changes is made to the web page and requires an alert
// Of-course you could call this is Keypress event of a text box or so...
}
function releaseDirtyFlag()
{
needToConfirm = false; //Call this function if dosent requires an alert.
//this could be called when save button is clicked
}
window.onbeforeunload = confirmExit;
function confirmExit()
{
if (needToConfirm)
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
credit goes to this forum (http://forums.devarticles.com/javascript-development-22/how-to-stop-browser-from-closing-using-javascript-8458.html)
matthewjumps
September 12th, 2007, 02:33 AM
thankyou this is perfect :)
a question arises now - currently the "return" is used to display a dialogue box that the user must confirm wether they want to navigate away from the page.
is there a way of bypassing this, i want to display a dialogue box in flash so im hoping i can use javascript to communicate with flash somehow and get flash to display the dialogue box. so the first thing im wondering is can i use something other than "return" to stop the page unloading, but not display a dialogue?
matthewjumps
September 16th, 2007, 10:25 PM
*bumps* :pirate3:
Pneumonic
September 18th, 2007, 05:16 PM
Try that ->http://www.kirupa.com/developer/flash8/video/popUpWindows.htm
matthewjumps
September 19th, 2007, 04:00 AM
thanks but thats a javascript popup tutorial, im looking for a way to stop the browser window/tab from closing and instead trigger an event in the fullscreen flash movie.
Pneumonic
September 19th, 2007, 02:06 PM
Thought it would help. Sorry.
I guess my question is if you're using flash for the popup (you are aren't you?) why wouldn't you capture them putting focus elsewhere and/or clicking on the X that way you don't need to make the call to js. See the problem I see with using js is this:
user does something; user clicks x; javascript gets called and sets variable; how does flash know that it should now look to the js at that specific time to see if the variable is 1 instead of 0.
Maybe I'm making this too hard, but that's just what my thinking is based upon what you've said so far. If I'm wrong tell me.
wes_design
September 19th, 2007, 04:50 PM
maybe this works ..
http://www.kirupa.com/forum/showthread.php?t=78131
More uptodate thread:
http://www.kirupa.com/forum/archive/index.php/t-252737.html
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.