This is the code that I used to set the onbeforeunload event and it works for Chrome.
The order it was written is crucial though else it wouldnt work.
<script>
var unloadEvent = function (e) {
var confirmationMessage = "Warning: Leaving this page will result in any unsaved data being lost. Are you sure you wish to continue?";
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
};
window.addEventListener("beforeunload", unloadEvent);
</script>