History.js 的实现
我正在尝试为我的 ajax 站点实现 History.js,以便我可以使用前进和后退按钮,甚至书签。然而,这个例子@ https://github.com/browserstate/History.js/ 让我有点困惑如何实施。有谁有关于如何使用它的简单教程或示例。我们可以用来启动该示例的一个示例是导航链接,例如
<script type="text/javascript">
window.onload = function()
{
function1();
};
<ul>
<li><a href="javascript:function1()">Function1</a></li>
<li><a href="javascript:function2('param', 'param')"</a></li>
</ul>
I am trying to implement History.js for my ajax site so that I can use forward and back buttons and even bookmarks. However the example @ https://github.com/browserstate/History.js/ has me a bit confused as into how to implement it. Does anyone have a simple tutorial or example on how to use this. An example we can use to start the example is a navigation link such as
<script type="text/javascript">
window.onload = function()
{
function1();
};
<ul>
<li><a href="javascript:function1()">Function1</a></li>
<li><a href="javascript:function2('param', 'param')"</a></li>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很难完全理解如何使用 History.js。以下是他们 wiki 中的一些代码以及我的解释注释:
1。获取对history.js 对象的引用
获取对History.js 对象引用window.History (Capitol 'H') 的引用。
要检查 HTML5 历史记录是否已启用,请检查 History.enabled。如果不是的话,History.js 仍然可以使用哈希标签。
2.侦听历史更改并从此处更新您的视图
要侦听历史状态更改,请绑定到 Adapter 对象的 statechange 事件。
3.使用推送或替换操作历史状态
要将状态添加到历史记录,请调用pushState。这将在历史堆栈的末尾添加一个状态,该状态将触发“statechange”事件,如上所示。
要将状态替换到历史记录中,请调用replaceState。这将替换历史堆栈中的最后一个状态,该状态将触发“statechange”事件,如上所示。
pushState 和replaceState 的区别在于pushState 会向历史列表中添加一个状态,replaceState 会覆盖最后一个状态。
注意:pushState 和replaceState 会序列化数据对象,因此请使用最少的数据。
4.如果您想添加额外的 ui 以便用户能够导航历史记录,请使用导航命令
使用 back 和 go 通过代码导航历史记录。
附加:使用具有历史记录的“a”标签
要使用具有历史记录的“a”标签,您需要拦截点击事件并使用 event.preventDefault() 方法阻止浏览器导航。
示例:
我希望这会有所帮助。
I had trouble fully understanding how to use History.js. Here is some code from their wiki with my comments for explanation:
1. Get a reference to the history.js object
To get a reference to the History.js object reference window.History (Capitol 'H').
To check if HTML5 history is enabled check History.enabled. History.js will still work using hash tags if it is not.
2. Listen for history changes and update your view from here
To listen for history state changes bind to the statechange event of the Adapter object.
3. Manipulate history states by using push or replace
To add a state to the history, call pushState. This will add a state to the end of the history stack which will trigger the 'statechange' event as shown above.
To replace a state to the history, call replaceState. This will replace the last state in the history stack which will trigger the 'statechange' event as shown above.
The difference between pushState and replaceState is that pushState will add a state to the history list, replaceState will overwrite the last state.
NOTE: pushState and replaceState serialize the data object, so use minimal data there.
4. If you want to add additional ui for the user to be able to navigate the history, use the navigation commands
Use back and go to navigate the history via code.
Additional: Using "a" tags with history
To use "a" tags with history you will need to intercept click events and prevent the browser from navigating by using the event.preventDefault() method.
Example:
I hope this helps.
请参考这个问题:在 jquery ajax 调用上实现“后退按钮”
一些与 RSH 相关的更多信息
如何为 Ajax 历史实现 RSH(真正简单的历史)返回按钮
Refer this question: Implementing "Back button" on jquery ajax calls
Some more info related to RSH
How to implement RSH (Really Simple History) for Ajax History Back Button