tags:

views:

199

answers:

2

Got a window.history.go(1) to stop the user hitting the back button, but was wondering if I could check to see if a forward history exists before doing it, so I can display a popup warning the user not to press the back button.

I know you can get the history length, but is there a way to get the current position in the history list? Or some other way of doing this...

+1  A: 

AFAIK you won't be able to access the history from JavaScript. It will be a security hole and most of the browsers won't allow that. Probably there might be a workaround in IE by using ActiveX.

Found this entry which might be useful to you

window.history

There is a next property.

Returns the URL of the next item in the session history This property is not available to web content and is not supported by other browsers.

For security reasons the History object doesn't allow the non-privileged code to access the URLs of other pages in the session history, but it does allow it to navigate the session history.

There is no way to clear the session history or to disable the back/forward navigation from unprivileged code. The closest available solution is the location.replace() method, which replaces the current item of the session history with the provided URL.

rahul
Yeah it does look that way, but it's also confusing that it would let you get the number of items in the list and stuff like that.
SLC
In response to your edit, I don't see how it is a security hole to be able to check if a function you wish to call will succeed or not...
SLC
Its not about a function call, but its about the user security being exposed to an unknown website.
rahul
A: 

Don't break the back (or forward) button.

  1. You don't know exactly what they might do without knowing every possible user's setup (including plugins, etc.)
  2. You still have to do something mildly intelligent (e.g. not trash your database) if scripting is disabled, or disabled then re-enabled, etc.
  3. Because you already have to handle #2, don't even worry about figuring out how to do #1 everywhere for everyone, and spend more time to make #2 work better.
  4. Now your site works with the back button.
Roger Pate