I have web app which contains iframe. In main part client communicates with server by ajax, additionally i added beforeunload = function(){ return "something";} , so when user clicks browser back button alert will appear, when user clicks leave the page, he will lose session and login page will show. In iframe part i use servlet, so when user clicks browser back button alert won't appear because only iframe page will unload. Now what i want to achieve. While user is manipulating in iframe, when he clicks browser back button then alert should appear and when user click leave page i want to log off whole app. When he clicks don't leave page inside iframe shouldn't reload.
I tried to solve this using two events beforeunload and unload. This is inside iframe:
var confirm = false;
var clicked = true;
$(window).on('beforeunload', function(evt){
if(confirm){
return "Text";
} else {
clicked = false;
}
});
$(window).on('unload',function(evt){
if(clicked){
$(this).off('beforeunload');
window.parent.location = window.parent.location + "/error";
}
});
Now when user manipulating in iframe clicks back button browser, alert appears. When clicks leave page is working good, log out. But when clicks don't leave, the page in iframe will back to earlier and i don't why:(. I want to prevent it.