window.onbeforeunload is working with the Firefox and IE 6 but it does not work with IE 7 (in the same manner with other browsers). So I was able to find a solution for that.
var goodExit = false;
window.onbeforeunload = confirmExit;
function = confirmExit()
if(your any condition)
goodExit = true; //then that confirmation will not be asked
return 'Are you sure to navigate away ?'; //this will display on confirmation
//there should not any returns on the function confirmExit.
You can make the goodExit true when the page is doing submit or any other that we do not need a confirmation.And also do not assign true or false to window.onbeforeunload. Because it may lead to a script error on IE 7.