I have a form page and I want to alert user if he leaves it without saving the form. Typically I can achieve this purpose by stating a function window.onBeforeUnload.
window.onBeforeUnload = function(){return 'Please save the form before navigating';}
But it seems doesn't work in my project using Meteor and Iron Router. Any suggestion?
This is maybe a hacky version, but it works:
isUnsaved = ->
  return unless @ready()
  unless confirm 'Do you really want to leave the page?'
    Router.go(@url)
Router.onStop isUnsaved,
  only: [
    'editPost'
  ]
Old post, but just solved it. Here is my solution :
Router.route('/myPath', {
    unload: function (e, obj) {
        if(Session.get("hasChanged")){
            if (confirm("Are you sure you want to navigate away ?")) {
                // Go and do some action
            }else{
                // Redirect to itself : nothing happend
                this.redirect('/myPath');
           }
        }
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With