TypeError:'replaceState'在未实现接口历史记录的对象上调用

Pet*_*ter 2 javascript browser-history

我正在看这段JavaScript代码

if (history) {
    var action = settings.replaceState ? history.replaceState : history.pushState;
    if (action) {
        // next line throws the error
        action(null, null, window.location.pathname + window.location.search + '#' + hash);
    }
}
Run Code Online (Sandbox Code Playgroud)

settings.replaceState == true

微软的最新东西给了我

无效的呼叫对象

在Chrome中,同一段代码会抛出该错误

未捕获的TypeError:非法调用

我在Firefox中收到此错误

TypeError:'replaceState'在未实现接口历史记录的对象上调用。

当我调试历史记录时,它看起来应该是正确的,并且有一个包含此方法的原型。

除了不同的错误消息,谁能告诉我这是怎么回事?

Pat*_*ans 5

history通过将方法分配给变量,您失去了执行上下文。您需要history使用调用 / 绑定将执行上下文设置回

action.call(history,null, null, 
            window.location.pathname + window.location.search + '#' + hash);
Run Code Online (Sandbox Code Playgroud)