«

»

Jun 05

OnBeforeUnload fun

Yesterday, we have encountered issues regarding window.onbeforeunload function. We have implemented checking of json transport issues, but this throws errors, when the page is beeing unloaded and we needed to hide these errors.

So we implemented onBeforeUnload function, however we needed to preserve any previous onBeforeUnload functions. The issue here is that IE does not understand, that return value NULL is supposed to mean ‘Do not show the confirm’.

var origOnBeforeUnload = window.onbeforeunload;
window.onbeforeunload = function(event) {
  if(typeof origOnBeforeUnload === 'function') {
    return origOnBeforeUnload(event);
  }
}

We needed to implement little more robust checking, and when we do not want to show a message, you need to not return anything.

var origOnBeforeUnload = window.onbeforeunload;
window.onbeforeunload = function(event) {
  //do any code you want to
  if(typeof origOnBeforeUnload === 'function') {
    var out = origOnBeforeUnload(event);
    if(typeof out !== 'undefined' && out !== null && out !== false)
      return out;
    }
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>