Office.js使浏览器历史记录功能无效,打破历史记录使用情况

时间:2017-03-07 07:45:10

标签: office-js

office.js的官方版本可在此处获取:

https://appsforoffice.microsoft.com/lib/1/hosted/office.js

代码中包含以下几行:

window.history.replaceState = null;
window.history.pushState = null;

这会破坏我的Excel加载项中的一些历史记录功能(我正在使用reactreact-router

为什么office.js会使这些历史记录功能无效?我在文档中找不到任何解释。

3 个答案:

答案 0 :(得分:7)

Excel中使用的浏览器控件不支持历史记录API,如果未取消了replaceState和pushState,则可以做出反应,但在调用时总是抛出异常。在新的浏览器控件可用之前,您需要切换到基于散列的路由或使用填充历史API。如果在office.js之后包含脚本引用,则https://github.com/devote/HTML5-History-API似乎有效。

答案 1 :(得分:3)

这对我有用-在office-js删除对象之前先对其进行缓存:

<script type="text/javascript">
    // Office js deletes window.history.pushState and window.history.replaceState. Cache them and restore them
    window._historyCache = {
        replaceState: window.history.replaceState,
        pushState: window.history.pushState
    };
</script>

<script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>

<script type="text/javascript">
    // Office js deletes window.history.pushState and window.history.replaceState. Restore them
    window.history.replaceState = window._historyCache.replaceState;
    window.history.pushState = window._historyCache.pushState;
</script>

答案 2 :(得分:0)

我的Windows版本是10 Pro,默认浏览器是Edge 42.17134.1.0。但是Outlook运行加载项的右侧栏使用的是旧的IE10引擎;((Windows中也使用IE10作为浏览器,在Windows中也是如此。)我不知道这适用于所有Windows,或者对于我的版本来说是特定情况。 supports _temporaryhistory.replaceState,但是在Outlook中,这些方法存在问题,因此简单的还原对我不起作用。

使用缓存history.pushStatehistory.replaceState的简单解决方案对我不起作用。在内部带有IE10的Outlook中,当我的代码调用history.pushStatehistory.replaceState时出现一些意外错误。但是我发现了一件有趣的事情。如果抑制错误,他们将继续工作。

所以我的解决方法是:

history.pushState

注意:在运行与此API一起使用的任何代码之前,我应该替换 function isIE10 () { return !!document.documentMode } // Office js deletes window.history.pushState and window.history.replaceState. Cache them and restore them // Also there is an issue in Windows Outlook with `pushState` and `replaceState`. They throw an error but in the same time do their expected work // So I suppress errors for IE10 (we use it inside Window Outlook) window._historyCache = { replaceState: function (originalReplaceState) { return function () { try { return originalReplaceState.apply(window.history, arguments) } catch (e) { if (isIE10()) { console.warn("Unexpected error in 'window.history.replaceState', but we can continue to work :)"); return false; } throw(e); } } }(window.history.replaceState), pushState: function (originalFunction) { return function () { try { return originalFunction.apply(window.history, arguments) } catch (e) { if (isIE10()) { console.warn("Unexpected error in 'window.history.pushState', but we can continue to work :)"); return false; } throw(e); } } }(window.history.pushState) }; // In Window Outlook we have issue with 'replaceState' and 'pushState. So replaced it by wrapped version. window.history.replaceState = window._historyCache.replaceState; window.history.pushState = window._historyCache.pushState; //include the main code with react-router //include Office.js Office.initialize = function () { // Office js deletes window.history.pushState and window.history.replaceState. Restore them window.history.replaceState = window._historyCache.replaceState; window.history.pushState = window._historyCache.pushState; // Now you can start initialize&&run your application .... } history.replaceState。就我而言,它是反应路由器。