Skip to content

Instantly share code, notes, and snippets.

@unknownuser88
Created January 6, 2014 15:37
Show Gist options
  • Save unknownuser88/8284572 to your computer and use it in GitHub Desktop.
Save unknownuser88/8284572 to your computer and use it in GitHub Desktop.
OnBeforeUnload
var OnBeforeUnload = (function(){
var
FDUM = new Function,
AFFIRM = function(){ return true; };
var _reg = function(msg,opts){
opts = opts || {};
var
pid = null,
pre = typeof opts.prefire == 'function' ? opts.prefire : FDUM,
callback = typeof opts.callback == 'function' ? opts.callback : FDUM,
condition = typeof opts.condition == 'function' ? opts.condition : AFFIRM;
window.onbeforeunload = function(){
return condition() ? (pre(),setTimeout(function(){ pid = setTimeout(callback,20); },1),msg) : void 0;
}
window.onunload = function(){ clearTimeout(pid); };
}
var _unreg = function(){ window.onbeforeunload = null; }
return {
register : _reg,
unregister : _unreg
};
})();
OnBeforeUnload.register('Hello!',{
condition:function_that_returns_true_to_fire_event_false_to_ignore,
prefire:function_to_call_on_page_exit_attempt,
callback:function_to_call_if_user_stays
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment