如何通过Console识别PushState URL更新?

时间:2014-08-27 15:58:51

标签: javascript pushstate history.js

如果有一种方法可以使用console / inspect-element / dev-tools确定是否已通过PushState更新了URL,请告诉我吗?

例如,如果我转到http://html5.gingerhost.com/并点击“Seatle”,“London”和& “纽约”然后我知道它正在通过PushState进行更新,但我怎么能通过控制台'知道'呢?

正在开发的网站正在使用PushState和amp;的混合。 ReplaceState,我渴望能够在开发工具控制台中看到它们。

1 个答案:

答案 0 :(得分:0)

这仅用于调试目的,但您可以使用自己的API覆盖默认API。

调试历史事件的快速而苛刻的方法:

(function(window, listenTo) {
    var history = window.history;
    // replaces default APIs with function that also logs the arguments
    listenTo.forEach(function(key) {
        var original = history[key];
        history[key] = function() {
            console.log('history.' + key, arguments);
            original.apply(history, arguments);
        };
    });
    // add a listener to the popstate event for debugging purposes
    window.addEventListener('popstate', function() {
        console.log('onpopstate', arguments);
    });
})(window, ['pushState', 'replaceState']);