Faiyet
Faiyet

Reputation: 5471

block back button - ignore on click of button

I have a screen where I don't want the user to click on the back button. Now I am doing an ajax call to show that screen. There a button on it, when the click event is fired I want to redirect to the home page. Here its is.

$.post("<?php echo base_url();?>register/citizen", myobj2, function(data){
    $("body").empty().append(data);
    window.location.hash="no-back-button";
    window.location.hash="Again-No-back-button";
    window.onhashchange=function(){window.location.hash="no-back-button";}
    window.onbeforeunload = function() {
        return "If you leave this page you will have to register again.";
    }
});

On the page that comes in I have

$(document).ready(function(){
    $("#localfinal").unbind('click').on("click", function(){
        //$().redirect('<?php //echo base_url();?>');
        window.location(<?php echo base_url();?>);           
    });
});

When the user clicks the button, I get the message about leaving the page. Instead I just want to redirect to the base url (home);

Any ideas ?

Upvotes: 1

Views: 192

Answers (2)

BlackPOP
BlackPOP

Reputation: 5737

Try this

<SCRIPT type="text/javascript">
window.history.forward();
function noBack() { window.history.forward(); }
</SCRIPT>
 </HEAD>
<BODY onload="noBack();"
onpageshow="if (event.persisted) noBack();" onunload="">

Upvotes: 0

A. Wolff
A. Wolff

Reputation: 74420

Reset onbeforeunload handler, btw, i don't think you need to unbind() click handler:

$("#localfinal").on("click", function(){
        window.onbeforeunload = $.noop;
        window.location(<?php echo base_url();?>);           
    });

Upvotes: 1

Related Questions