History.js 的实现

发布于 2024-12-10 05:15:07 字数 553 浏览 0 评论 0原文

我正在尝试为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕巷 2024-12-17 05:15:07

我很难完全理解如何使用 History.js。以下是他们 wiki 中的一些代码以及我的解释注释:

1。获取对history.js 对象的引用

获取对History.js 对象引用window.History (Capitol 'H') 的引用。

var History = window.History;

要检查 HTML5 历史记录是否已启用,请检查 History.enabled。如果不是的话,History.js 仍然可以使用哈希标签。

History.enabled

2.侦听历史更改并从此处更新您的视图

要侦听历史状态更改,请绑定到 Adapter 对象的 statechange 事件。

History.Adapter.bind(window,'statechange',function(){ 
    var State = History.getState(); 
    History.log(State.data, State.title, State.url);
});

3.使用推送或替换操作历史状态

要将状态添加到历史记录,请调用pushState。这将在历史堆栈的末尾添加一个状态,该状态将触发“statechange”事件,如上所示。

History.pushState({data:"any kind of data object"}, "State Title", "?urlforthestate=1"); 

要将状态替换到历史记录中,请调用replaceState。这将替换历史堆栈中的最后一个状态,该状态将触发“statechange”事件,如上所示。

History.replaceState({data:"any kind of data object"}, "State Title", "?urlforthestate=1"); 

pushState 和replaceState 的区别在于pushState 会向历史列表中添加一个状态,replaceState 会覆盖最后一个状态。

注意:pushState 和replaceState 会序列化数据对象,因此请使用最少的数据。

4.如果您想添加额外的 ui 以便用户能够导航历史记录,请使用导航命令

使用 back 和 go 通过代码导航历史记录。

History.back();
History.go(2);

附加:使用具有历史记录的“a”标签

要使用具有历史记录的“a”标签,您需要拦截点击事件并使用 event.preventDefault() 方法阻止浏览器导航。

示例:

<a href="dogs/cats" title="Dogs and cats">Dogs and Cats</a>

$('a')​.click(function(e){
    e.preventDefault();
    var url = $(this).attr('href');
    var title = $(this).attr('title');
    History.pushState({data:title}, title, url);
})​;

我希望这会有所帮助。

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').

var History = window.History;

To check if HTML5 history is enabled check History.enabled. History.js will still work using hash tags if it is not.

History.enabled

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.

History.Adapter.bind(window,'statechange',function(){ 
    var State = History.getState(); 
    History.log(State.data, State.title, State.url);
});

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.

History.pushState({data:"any kind of data object"}, "State Title", "?urlforthestate=1"); 

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.

History.replaceState({data:"any kind of data object"}, "State Title", "?urlforthestate=1"); 

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.

History.back();
History.go(2);

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:

<a href="dogs/cats" title="Dogs and cats">Dogs and Cats</a>

$('a')​.click(function(e){
    e.preventDefault();
    var url = $(this).attr('href');
    var title = $(this).attr('title');
    History.pushState({data:title}, title, url);
})​;

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文