• 0

[JavaScript] prevent browser closing


Question

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

  • 0

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

  • 0

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

  • 0

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

  • 0

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>

&lt;script type="text/javascript"&gt;
window.onunload = saveStuff;
&lt;/script&gt;

or with the jQuery library

&lt;script type="text/javascript"&gt;
$(window).unload(saveStuff);
&lt;/script&gt;

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.