BearBlog: Prevent accidentally leaving 'New Post' page (improved)
I previously posted this solution. The old solution did the page-exit-prevention on any dashboard page iirc.
The new solution ONLY prevents page exit if you're on the 'new post' or 'edit post' page, by checking if the current url contains the string '/dashboard/posts/'.
Visit your dashboard at https://bearblog.dev/dashboard/customise/ and put in the following code under 'Dashboard footer content':
<!-- prevent accidental page exit -->
<script type="text/javascript">
window.onbeforeunload = function(event) {
console.log(event.originalTarget);
if (event.originalTarget.activeElement.getAttribute('type')=='submit'
|| event.originalTarget.activeElement.innerText=='Delete'){
console.log("Allow page exit because it's a submission or deletion.");
} else {
const url = window.location.href;
if (url.includes('/dashboard/posts/')){
console.log("Confirm page exit");
return "Are you sure you want to navigate away?";
} else {
console.log("Allow page exit because it's not a 'new post' or 'edit post' page.");
}
}
}
</script>