Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


How to disable browser back button?

Posted on 2010-09-16
Medium Priority
Last Modified: 2012-05-10
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:  http://mysite.com/category.php?page=1

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.
Question by:proteam4
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +5
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33691606
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

Expert Comment

ID: 33691613
There is no straight forward approach.
But you may try by using the apporach given in http://www.boutell.com/newfaq/creating/backbutton.html

Author Comment

ID: 33691647

I am already using following:


but no solution :(
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33691656
ok. This makes sure that current page is not cached, did you set that in previous pages also?
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33691687
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 http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26477105.html

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>
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33691702
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?

Author Comment

ID: 33691767

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

Author Comment

ID: 33691819

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

1st page load: http://mysite.com/category.php?page=1
2nd page load: http://mysite.com/category.php?page=2
3rd page load: http://mysite.com/category.php?page=3
4th page load: http://mysite.com/category.php?page=4
5th page load: http://mysite.com/category.php?page=5
6th page load: http://mysite.com/category.php?page=6
7th page load: http://mysite.com/category.php?page=7
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.
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 33691840
Hello proteam4,

Following is a link that might help you.


LVL 82

Expert Comment

ID: 33691913
Check here too : http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript

section : If you MUST control History

Author Comment

ID: 33692081
No solution guys. I tried almost all. :(
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33693204
Did you try mine at all?
LVL 14

Accepted Solution

sam2912 earned 1500 total points
ID: 33698223

$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
   header('Location: http://mysite.com/category.php?page='.$_SESSION["lastPage"]);

Open in new window

LVL 75

Expert Comment

by:Michel Plungjan
ID: 34204172
split - accept http:#a33691687 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34204881

Expert Comment

ID: 34255646
Starting the close procedure to accept the Expert's comment as the solution.

Experts Exchange Moderator

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question