简体   繁体   English

如何查看Javascript中是否存在转发历史记录? (禁用后退按钮)

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

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. 有一个window.history.go(1)来阻止用户按下后退按钮,但是想知道我是否可以检查是否存在前进历史记录,所以我可以显示弹出警告用户不要按返回键。

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. AFAIK您将无法从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. 可能在IE中可能有使用ActiveX的解决方法。

Found this entry which might be useful to you 找到此条目可能对您有用

window.history 的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. 返回会话历史记录中下一个项目的URL此属性不可用于Web内容,其他浏览器不支持此属性。

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. 出于安全原因,History对象不允许非特权代码访问会话历史记录中其他页面的URL,但它允许它导航会话历史记录。

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. 最接近的可用解决方案是location.replace()方法,该方法用提供的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 (eg 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. 因为你已经必须处理#2,所以甚至不用担心如何在每个地方找到#1,并花更多的时间让#2更好地工作。
  4. Now your site works with the back button. 现在您的网站使用后退按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM