+ Start a Discussion
VIGNESH BALASUBRAMANIANVIGNESH BALASUBRAMANIAN 

Prevent go back to previous page in VF

Hi Everyone,

My requirements is when I click back button on browser from VF page error should be thrown.And Previous page should not be viewed.

Thanks,
Vignesh.B
Balayesu ChilakalapudiBalayesu Chilakalapudi
Try using javascript in your VF page like this,
<apex:page>   
<body onLoad="noBack()">
     <script type="text/javascript"> 
            window.history.forward();      
            function noBack()          
            {               
               window.history.forward();    
            } 
     </script>
</body>
</apex:page>

Let us know if it helps.
karthikeyan perumalkarthikeyan perumal
Hello, 

This is browser behaviour rather than anything to do with Salesforce/Visualforce, so you'd need to trap the backspace key in the browser through JavaScript and block the standard behaviour.

There's more discussion and code at :http:// http://stackoverflow.com/questions/1495219/how-can-i-prevent-the-backspace-key-from-navigating-back (http:// http://stackoverflow.com/questions/1495219/how-can-i-prevent-the-backspace-key-from-navigating-back)
 
// Prevent the backspace key from navigating back.
$(document).unbind('keydown').bind('keydown', function (event) {
    if (event.keyCode === 8) {
        var doPrevent = true;
        var types = ["text", "password", "file", "search", "email", "number", "date", "color", "datetime", "datetime-local", "month", "range", "search", "tel", "time", "url", "week"];
        var d = $(event.srcElement || event.target);
        var disabled = d.prop("readonly") || d.prop("disabled");
        if (!disabled) {
            if (d[0].isContentEditable) {
                doPrevent = false;
            } else if (d.is("input")) {
                var type = d.attr("type");
                if (type) {
                    type = type.toLowerCase();
                }
                if (types.indexOf(type) > -1) {
                    doPrevent = false;
                }
            } else if (d.is("textarea")) {
                doPrevent = false;
            }
        }
        if (doPrevent) {
            event.preventDefault();
            return false;
        }
    }
});

Hope this will work. 

Thanks
karthik
 
VIGNESH BALASUBRAMANIANVIGNESH BALASUBRAMANIAN
Hi Everyone,

@ Bala & Karthik

Thanks for your replies.I found a Java Script which satisfied ​ my requirement..
<script type = "text/javascript" >
    function preventBack(){window.history.forward();}
    setTimeout("preventBack()", 0);
    window.onunload=function(){null};
    </script>
Thanks,
Vignesh.B