Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Javascript disable back button or replace URL when loading page

 
K. Tsang
Bartender
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
K. Tsang
Bartender
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After going through the coding, I managed to eliminate the exceptions when back button is clicked.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It'd be great if you could post a summary of the type of problems you found.
 
K. Tsang
Bartender
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic