The correct way to display the alert is to simply return a string. Don't call the alert()
method yourself.
<script type="text/javascript">
$(window).on('beforeunload', function() {
if (iWantTo) {
return 'you are an idiot!';
}
});
</script>
See also: https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload