<html>
<head>
<script type="text/javascript">
window.onbeforeunload = beforeUnload;

function beforeUnload(){
        return "Are you sure you want to leave?";
}
</script>
</head>
<body>
Please stay on this page!
</body>
</html>

-----------------------------------------------------------
좀 줄여서 쓰고 싶으면

window.onbeforeunload = function()
{
        return "Are you sure you want to leave?";
}
이렇게 써도 되요.


보통은 이렇게 쓰지만..
window.onbeforeunload = function() {
        return "Are you sure you want to leave?";
}