window.onbeforeunload = function () {
return "Are you sure that you want to leave this page?";
}
Demo page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Onbeforeunload test</title>
</head>
<body>
<p>This is a test of onbeforeunload. Please click this link to <a href="http://www.google.com">google.com</a></p>
<script type="text/javascript">
window.onbeforeunload = function () {
return "Are you sure that you want to leave this page?";
}
</script>
</body>
</html>
Google’s Gmail for example uses it very wisely. When you are writing an email it says that your message has not been sent if you try to close the window part-way through.
I agree that there are a few sites where it is useful but its misuse is so widespread that either disabling it completely or automatically confirming without bothering to read it are probably very common and so it doesn’t necessarily serve its intended purpose even when used appropriately.