EDIT-2: None of the answers seem to work. Not even the one I previously marked as the answer of this question. Any help is appreciated. Thanks.
The reason on using the return:false;
is well explained on this other question.
For the other issue, you can check for the referrer to see if it is empty:
function backAway(){
if (document.referrer == "") { //alternatively, window.history.length == 0
window.location = "http://www.example.com";
} else {
history.back();
}
}
Back Button Here.