0

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user1718468
  • 11
  • 2
  • 1

1 Answers1

0

One cannot disable the browser back button functionality only thing that can be done is prevent them.

Below is the JavaScript snippets that needs to be placed in the head section of the page where you don’t want the user to revisit using the back button

<script type = "text/javascript" >

   function preventBack(){window.history.forward();}

    setTimeout("preventBack()", 0);

    window.onunload=function(){null};

</script>

Suppose there are two pages Page1.aspx and Page2.aspx and Page1.aspx redirects to Page2.aspx

Hence to prevent user from visiting Page1.aspx using Back Button you will need to place the above script in the head section of Page1.aspx as shown below.

enter image description here

Here is the original article: http://www.aspsnippets.com/Articles/Disable-Browser-Back-Button-Functionality-using-JavaScript.aspx

Alfred
  • 21,058
  • 61
  • 167
  • 249