开发者

How can I check to see if a forward history exists in Javascript? (Disabling back button)

开发者 https://www.devze.com 2022-12-11 16:35 出处:网络
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 开发者_Python百科a popup wa

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 开发者_Python百科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...


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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号