本文介绍了历史回到JavaScript的钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法提供一个钩子,比如onHistoryBack?我目前正在使用history.js

Is there a way to provide a hook such as onHistoryBack? I'm currently using history.js with

History.Adapter.bind (window, 'statechange', function () {});

但是我没有办法询问用户是否提供了history.back()或者是否是History.pushState()调用..任何想法?

But I have no way to ask if user presed history.back() or if it's result of a History.pushState() call.. any idea?

推荐答案

我这样做的方法是将变量设置为true每次点击实际网站。然后statechange事件将检查此变量。如果是真的,他们在网站上使用了一个链接。如果它是假的,他们点击浏览器后退按钮。

The way I did this was to set a variable set to true on each click on the actual site. The statechange event would then check this variable. If it was true, they had used a link on the site. If it was false, they had clicked the browsers back button.

例如:

var clicked = false;

$(document).on('click','a',function(){

    clicked = true;

    // Do something

});

History.Adapter.bind (window, 'statechange', function () {

    if( clicked ){
        // Normal link
    }else{
        // Back button
    }

    clicked = false;

});

希望有所帮助:)

这篇关于历史回到JavaScript的钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 06:21