paxa Posted January 10, 2011 Share Posted January 10, 2011 hi. i'm trying to prevent the a browser close when a user uses the famous combination alt+f4. i can capture the event ok, but the browser is still closed. this is my code: <html> <head> <title>teste keyboard</title> <script type="text/javascript" language="javascript"> function LimitaTeclaIE() { switch(event.keyCode){ case 116: event.returnValue= false; event.keyCode=0; alert("NOT SUPPORTED 116"); return false; case 82: if (event.ctrlKey) { event.returnValue= false; event.keyCode=0; alert("NOT SUPPORTED 82"); return false; } case 115: if(event.altKey){ event.returnValue= false; event.keyCode=0; alert("NOT SUPPORTED 115"); return false; } } } </script> </head> <body onKeyDown="LimitaTeclaIE();"> </body> </html> help would be appreciated thanks in advance Link to comment Share on other sites More sharing options...
0 Cupcakes Posted January 10, 2011 Share Posted January 10, 2011 Short answer: don't control what the user does with his browser. If we want to exit your site, we will exit your site. Controlling what we do OUTSIDE of your site is pretty damn terrible. Link to comment Share on other sites More sharing options...
0 paxa Posted January 10, 2011 Author Share Posted January 10, 2011 i was asking this because the browser close is done by a event captured by a flash object, also i'm using this event to save something in a moodle scorm database when event is captured. so if the user presses alt+f4 the event will not be captured and the data will not be saved. is any other way around this? Link to comment Share on other sites More sharing options...
0 jackwanders Posted January 11, 2011 Share Posted January 11, 2011 The long and short of it is that you can't stop a user from quitting their browser. Even if you could prevent ALT+F4 from shutting down the browser, they could always close it via Task Manager, or just shutting down their computer. You will never be able to fully prevent a user from doing something you don't want them to. The best solution is to inform the user of your desires, rather than attempting to override their behaviors. Link to comment Share on other sites More sharing options...
0 Calculator Posted January 12, 2011 Share Posted January 12, 2011 You really shouldn't try to prevent a user from closing your website. However, if you just want to save some data when the user leaves your page, you can add an unload handler. Trying to intercept key combinations is certainly not the right thing to do. For example, if you want a function saveStuff to be called, you could write: <body onunload="javascript:saveStuff();"> or with JavaScript block a the end of your <body> <script type="text/javascript"> window.onunload = saveStuff; </script> or with the jQuery library <script type="text/javascript"> $(window).unload(saveStuff); </script> If the saving process is handled by a Flash object though, you'll need to call this Flash method from that JavaScript function using some Flash APIs (google this). If your Flash object doesn't expose such methods... I'm afraid you're out of luck then. Link to comment Share on other sites More sharing options...
0 paxa Posted January 23, 2011 Author Share Posted January 23, 2011 thanks for the help it worked Link to comment Share on other sites More sharing options...
Question
paxa
hi.
i'm trying to prevent the a browser close when a user uses the famous combination alt+f4.
i can capture the event ok, but the browser is still closed.
this is my code:
<html> <head> <title>teste keyboard</title> <script type="text/javascript" language="javascript"> function LimitaTeclaIE() { switch(event.keyCode){ case 116: event.returnValue= false; event.keyCode=0; alert("NOT SUPPORTED 116"); return false; case 82: if (event.ctrlKey) { event.returnValue= false; event.keyCode=0; alert("NOT SUPPORTED 82"); return false; } case 115: if(event.altKey){ event.returnValue= false; event.keyCode=0; alert("NOT SUPPORTED 115"); return false; } } } </script> </head> <body onKeyDown="LimitaTeclaIE();"> </body> </html>help would be appreciated
thanks in advance
Link to comment
Share on other sites
5 answers to this question
Recommended Posts