• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Javascript disable back button or replace URL when loading page

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After going through the coding, I managed to eliminate the exceptions when back button is clicked.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic