I’m currently using the jquery-form-observe plugin which uses onbeforeunload to prompt the user about “unsaved” changes.
But I have a scenario where I need to trigger this on a button click: the button click ultimately leads to the page changing, but I want to prompt the user before they start the process that the button click kicks off…
So is there a way to trigger onbeforeunload through jQuery or otherwise?
I don’t know if there is a direct way to do this, but you could always emulate the browser’s confirmation box yourself. Here’s a simple function I cooked up based on the specs at MSDN:
Edit: In
jquery.formobserver.js, right after the definition offunction beforeunload(e) { ... }, add this line:Note the change in the original code:
window.onbeforeunloadhas been replaced byhandler.