I didn't have a lot of joy adding your code to anywhere on a page, however I tweaked it a bit to;
<script type="text/javascript">
function closeIt()
{
alert( "Any string value here forces a dialog box to \n appear before closing the window.");
}
window.onbeforeunload = closeIt();
</script>
If I was doing this I'd probably use the jQuery syntax, for best cross-browser compatibility;
<script>
$(window).unload( function () { alert("Bye now!"); } );
</script>
that all said, I'm really not sure you're gonna be all that happy with the effect on the app. It occurs whenever the user navigates away from the page, not just on a Back or Cancel button. So I suspect it occurs a lot more than you really want it to.
Still, try it out and let us know how it turns out.
cheers
Bruce