Edit in JSFiddle

$(document).mousemove(function(e){
  var pageCoords = "( " + e.pageX + ", " + e.pageY + " )";
  var clientCoords = "( " + e.clientX + ", " + e.clientY + " )";
  $("span:first").text("( e.pageX, e.pageY ) - " + pageCoords);
  $("span:last").text("( e.clientX, e.clientY ) - " + clientCoords);
});
$(document).mouseleave(function(e){
    $('#debug').html('leave')
    window.onbeforeunload =  function () {
        window.onunload =  function () {
            alert('bye')
        }
        return '您確定要離開?';
    }
});
$(document).mouseenter(function(e){
    $('#debug').html('enter')
    window.onbeforeunload =  null
    window.onunload =  null
});
<a href="http://mesak.wablog.info/blog">http://mesak.wablog.info/blog</a>
 <br>X<span></span><br>Y<span></span>
 <div id="debug"></div>