Solved

How to disable browser back button?

Posted on 2010-09-16
19
354 Views
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:

<script>
if(window.history.forward(1) != null)
                 window.history.forward(1);
</script>

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.
0
Comment
Question by:proteam4
  • 4
  • 4
  • 3
  • +5
19 Comments
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
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
http://www.codeave.com/html/code.asp?u_log=5080
0
 
LVL 2

Expert Comment

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

Author Comment

by:proteam4
Comment Utility
@gurvinder372:

I am already using following:

   
   
   

but no solution :(
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
ok. This makes sure that current page is not cached, did you set that in previous pages also?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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>
 
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
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?
0
 

Author Comment

by:proteam4
Comment Utility
vsosu:

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

Author Comment

by:proteam4
Comment Utility
gurvinder372:

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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:Amar Bardoliwala
Comment Utility
Hello proteam4,

Following is a link that might help you.

http://www.htmlgoodies.com/tutorials/buttons/article.php/3478911/Disabling-the-Back-Button.htm

Thanks.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Check here too : http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript

section : If you MUST control History
0
 

Author Comment

by:proteam4
Comment Utility
No solution guys. I tried almost all. :(
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Did you try mine at all?
0
 
LVL 14

Accepted Solution

by:
sam2912 earned 500 total points
Comment Utility

<?

session_start();

$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

else

   header('Location: http://mysite.com/category.php?page='.$_SESSION["lastPage"]);

?>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
split - accept http:#a33691687
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
0
 

Expert Comment

by:ModernMatt
Comment Utility
Starting the close procedure to accept the Expert's comment as the solution.

ModernMatt
Experts Exchange Moderator
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now