How to disable browser back button?

I want to disable back button. I have searched EE already but didn't find solution yet. Actually I am using the following code:

if(window.history.forward(1) != null)

Open in new window

but that works and behave incorrectly when I try to go back to last 2nd 3rd or 4th and so on back from the back button history of visited page.

I have a dynamic page like:

When I keep on going till page 3, 4, 5, 6, and so on. Lets say I am on page 6 and want to go back to page 1 with the help of back button history of visited pages. It first loads page 1 then 2, then 3, then 4 , then 5 and then 6. I want to load page 6 directly because I was on page 6 and I tried going back to page 1. It should not traverse the page from 1-6.

Please help.
Who is Participating?
Samuel LiewConnect With a Mentor Commented:

$currentPage = intval($_GET["page"]);

// Current page num is higher, save it to session
if(!isset($_SESSION["lastPage"]) || $currentPage >= $_SESSION["lastPage"])
   $_SESSION["lastPage"] = $currentPage;
// Current page num is lower, redirect to last page visited

Open in new window

Gurvinder Pal SinghCommented:
How about removing the previous pages from cache? see how to do it

Put this in your HTML code of your pages
<meta http-equiv="cache-control" content="no-cache" />

This will prevent the browser from caching any previous page

check this also
There is no straight forward approach.
But you may try by using the apporach given in
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

proteam4Author Commented:

I am already using following:


but no solution :(
Gurvinder Pal SinghCommented:
ok. This makes sure that current page is not cached, did you set that in previous pages also?
Michel PlungjanIT ExpertCommented:
Gurvinder, why do you keep talking about cache?
It does NOT remove the page from HISTORY so the page will still be under the back button, just not cached so it will still be fetched.

This is a duplicate of

1. do NOT try to break or disable a back button
2. instead either
  a) navigate using ajax
  b) open a new window

3. if you use location.replace(url) the current page is replaced in the history so

<a href="page2.html" onclick="location.replace(this.href); return false">Page2</a>
 <a href="page3.html" onclick="location.replace(this.href); return false">Page3</a>
 <a href="page4.html" onclick="location.replace(this.href); return false">Page4</a>
Gurvinder Pal SinghCommented:
If you put those tags in all the pages you do not wish to access via back button, why can't this issue be resolved?
proteam4Author Commented:

I tried it, but when I try to open any page from history, it keeps on infinite reload-loop.
proteam4Author Commented:

Actually I am having same page which loads dynamic content based upon the query string var page, for example:

1st page load:
2nd page load:
3rd page load:
4th page load:
5th page load:
6th page load:
7th page load:
and so on....

Lets say I am on page 6 and now I am selecting previous visiting page 1 using browser back history. It first loads page 1, then page 2, then page 3 then page 4 then page 5 and then last page 6.

Its ok in the case of 5-10 pages browsing. But, lets think where user visiting around 100+ pages and he/she selects page 1 from history or something like that then page 1 to 100 will get traversed and page load on server will be toooo much.
Amar BardoliwalaCommented:
Hello proteam4,

Following is a link that might help you.

Check here too :

section : If you MUST control History
proteam4Author Commented:
No solution guys. I tried almost all. :(
Michel PlungjanIT ExpertCommented:
Did you try mine at all?
Michel PlungjanIT ExpertCommented:
split - accept http:#a33691687 
Michel PlungjanIT ExpertCommented:
Starting the close procedure to accept the Expert's comment as the solution.

Experts Exchange Moderator
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.