aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Javascript disable back button or replace URL when loading page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Javascript disable back button or replace URL when loading page" Watch "Javascript disable back button or replace URL when loading page" New topic
Author

Javascript disable back button or replace URL when loading page

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2359
    
    7

Hello ranchers

I'm trying to disable back button OR replace the URL when loading the page. When the user loads a new page, I expect the browser's back button is disabled. I tried the following:


The above code will go into an infinite loop doing the window.location.replace().

The other approach I tried is:


The above using the history.forward() but this seems not what I expect.

Any other approach to achieve this? Thanks.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

What problem are you actually trying to solve? Whatever it is, trying to disable the back button is not the correct solution.

Are you also going to disable the keyboard shortcut? Context menu? Double-finger gestures on trackpads? What about tablets? Mapped buttons on trackballs? What about all the other ways to go back?

Not only is pretty near impossible, it's also a downright unfriendly practice. Please read: InterfaceDesignersRule1


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2359
    
    7

Thanks Bear for your input.

Well the actual problem is that when the web app session timed out ... sometimes there pops up a HTTP 500 error ranging from NullPointerException, ArrayOutOfBoundsException or some other exception when the user goes back a page. So I thought if I prevent the user going back then under such circumstances none of these exceptions will occur.

I understand so-called hacking the browser is not the way to go.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

The best approach would be to find out why the exceptions are occurring and fix that problem. Surely the stack traces will be useful to find out why the requests are failing when using the back button?

I'd also advise reading this article and paying particular attention to the PRG pattern. It will help to make sure that requests behave in a deterministic fashion.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2359
    
    7

After going through the coding, I managed to eliminate the exceptions when back button is clicked.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65



It'd be great if you could post a summary of the type of problems you found.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2359
    
    7

Sure Bear

My app writtten in JSF2 and uses managed bean (mainly view and session scoped). Well it depends on how you write the program the first round.

At first when user logs in a bunch of attributes are stored in session map. Nothing wrong with then. But in the code it does



This will throw NullPointerException if there is no session! because of part before toString() is null. Most of these needs changing.

All in all, checking and testing every page when session expires takes a long time.
 
 
subject: Javascript disable back button or replace URL when loading page