2

I want to change the function of the "back" browser button. I tried with this:

window.onbeforeunload = function() {
    show_page(last_page, 'not-search'); 
}

and it works, in part: it calls the function but the browser navigates to the previous page anyway. Is there a solution for that?

EDIT I need to do that:

index(ajax content) ---user click--->other ajax content(i call it A content)
A content --->back button ---->previous index content

  • 3
    Possible duplicate of [how to stop browser back button using javascript](http://stackoverflow.com/questions/12381563/how-to-stop-browser-back-button-using-javascript) – Sam Hanley Dec 09 '15 at 17:35
  • Possible duplicate of [Disable browser "Back" button](http://stackoverflow.com/questions/7011334/disable-browser-back-button) – try-catch-finally Dec 09 '15 at 17:40
  • i just updated my answer after i saw you've updated your question. – Aᴍɪʀ Dec 09 '15 at 17:53
  • You may want to reflect the state change of your page in the URL. You can archieve this by programmatically changing the [hash](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/hash) (you can listen to a hash change using the [`hashchange` Event](https://developer.mozilla.org/en-US/docs/Web/API/HashChangeEvent)). Alternatively you can use [pushstate](https://developer.mozilla.org/en-US/docs/Web/API/History/pushState) – try-catch-finally Dec 09 '15 at 17:56

3 Answers3

1

Return a string in your function to warn the user and give the option to cancel.

window.onbeforeunload = function() {
    show_page(last_page, 'not-search'); 
    return "please do not use the back button";
}

Completely disabling is both tricky and bad practise. Limiting the user in such a way could easily be seen as spam or unwanted behaviour.

edit

After I have read your edit, I would suggest to use history.pushState whenever you are modifying the page with ajax. That way the backbutton will navigate to an earlier state which is what you want. Take a look at the history API for all functions you need.

When the previous button is pressed it will trigger window.onpopstate function where you can handle to load the correct content. See example below:

window.onpopstate = function(event) {
  alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
-1

To stop user from going to previous page

add this js at bottom of page

<script language="text/javascript">
window.history.forward();
</script>
Alpesh Panchal
  • 1,723
  • 12
  • 9
  • Why would programmatically navigating forward on pageload (while there can't be any "positive" history entry yet) prevent back navigation? – try-catch-finally Dec 09 '15 at 17:39
  • If you press back button, it will go to previous page where window.history.forward(); will be executed and it will take user again to forward. So that way you can't go back by browser. if there are no positive history, this code will do nothing. – Alpesh Panchal Dec 09 '15 at 17:43
  • This line of code is only executed once when the page has loaded and not when another page loads or the current page is in the process of unloading. – try-catch-finally Dec 09 '15 at 17:45
-1

Take a look at this: http://www.irt.org/script/311.htm

Quote from the website above:

Easy answer - you can't.

Longer answer - you cannot totally remove the ability for the user to go back to the previous location, although you can make it harder.

After you add the update to your question:

If you are manipulating the content of the page using Ajax, you can update the history of the browser manually. You should also consider adding hashes at the end of the url.

For more information about the history API: https://developer.mozilla.org/en-US/docs/Web/API/History_API

Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52