From out of curiosity can i Control window.onbeforeunload event like check if the user decided to leave the page or stay in it and can I raise an alert or some function based on
You cannot get the result of onbeforeunload. Also, even if you somehow managed to figure out a way to get the result, you wouldn't be able to raise an alert. You could probably run another function.
The showModalDialog(), alert(), confirm() and prompt() methods are now allowed to do nothing during pagehide, beforeunload and unload events.