0

I want to disable back and front button of browsers. I have tried with the following code and Iam using onload for div tag and div tag id is order.

<script type="text/javascript">
   $(document).ready(function() {
       var back= noback();
         $("#order").load("back");
         function noback() {
        window.history.forward(-1);
        alert('backbtn');
      }
  setTimeout("noback()", 0);    
  alert('loading');
});
</script>

When i tested, alert backbtn is working fine but i can navigate to back page. Please help me how to make disable those buttons.

mounika
  • 35
  • 1
  • 2
  • 8

2 Answers2

2

That would not be a good approach to disable the browser back button rather you should add code to handle the functionality as to what happens when the user clicks on back button.

On approach could be to place this script in the head section of the page so as to dont allow the user to visit the page again

<script>
  function restrictback(){window.history.forward();}
  setTimeout("restrictback()", 0);
  window.onunload=function(){null};
</script>
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0
<SCRIPT type="text/javascript">
    window.history.forward();
    function noBack() { window.history.forward(); }
</SCRIPT>
</HEAD>
<BODY onload="noBack();"
    onpageshow="if (event.persisted) noBack();" onunload="">
Smruti Singh
  • 565
  • 1
  • 4
  • 14
  • here in the code Iam using div tag for onload..@smruthi – mounika Aug 27 '14 at 07:16
  • onload function doesnt work on div. The onload event can only be used on the document(body) itself, frames, images, and scripts. In other words, it can be attached to only body and/or each external resource. The div is not an external resource and it's loaded as part of the body, so the onload event doesn't apply there. CHeck this link http://stackoverflow.com/questions/4057236/how-to-add-onload-event-to-a-div – Smruti Singh Aug 27 '14 at 07:18
  • yes i know..i have seen this previously Smruthi..for that reason Iam giving id for div tag and using the id in $("#order").load("back"); Please have a look at my code. – mounika Aug 27 '14 at 07:22