0

I am using native.history.js to put a customised URL in the back button.

The script works fine. Now the issue is I want to make a page redirect when the refresh button is clicked; so i modified the script like this:

<script>
var back = 0;
$(window).bind('beforeunload', function () {
    if (confirm('Want to continue?')) {
        if (back == 1) {
            alert('back');
            //window.location.href = "<?=$$last_offer?>";
        } else {
            alert('refresh');
            //window.location.href = "<?=$actual_link;?>";
        }
    } else {
        // Do nothing!
    }
});

window.onpageshow = function (e) {
    if (e.persisted) {
        location.reload();
    }
};

window.onpopstate = function (event) {
    if (document.location.toString().indexOf("redir=1") > 0) {
        back = 1;
        window.location.href = "<?=$$last_offer?>";
    }
};
</script>

Issue is, the beforeunload function seems not working. What is the problem I can't fin?.
If I am clicking the back button, the page is taking to the desired page, so it works fine.
All I want is that, somehow the page refresh must work as I anticipated.

Saswat
  • 12,320
  • 16
  • 77
  • 156
  • What you can do in a beforeunload handler is very limited. http://stackoverflow.com/questions/6063522/dialog-box-runs-for-1-sec-and-disappears – Maurice Perry Nov 04 '14 at 08:34

1 Answers1

0

Try use

$(window).on('beforeunload', function(){
  return 'Are you sure you want to leave?';
});

Instead of bind, use on, i dont know what jquery version you use, but i will suggest "on".

Works fine here:

link

on jquery version 2.x(edge)

Jesper Kaae
  • 118
  • 1
  • 9
  • Did you realy go through my code, buddy? My question was, based on the return value, i want to navigate or not.... if back=1 and return is yes, user navigates to link1 and if back=0 and return is yes, user navigates to link2 – Saswat Nov 04 '14 at 08:36