Skip to content

Commit a606f8a

Browse files
authored
[all hosts] (Initialization) Document browser state method null workaround (#4991)
* Document browser state method null workaround * Add Stack credit
1 parent e80ca40 commit a606f8a

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

docs/develop/referencing-the-javascript-api-for-office-library-from-its-cdn.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Referencing the Office JavaScript API library
33
description: Learn how to reference the Office JavaScript API library and type definitions in your add-in.
4-
ms.date: 01/07/2025
4+
ms.date: 01/14/2025
55
ms.localizationpriority: medium
66
---
77

@@ -21,6 +21,31 @@ This will download and cache the Office JavaScript API files the first time your
2121
> [!IMPORTANT]
2222
> You must reference the Office JavaScript API from inside the `<head>` section of the page to ensure that the API is fully initialized prior to any body elements.
2323
24+
## Office.js-specific web API behavior
25+
26+
Office.js replaces the default [Window.history](https://developer.mozilla.org/docs/Web/API/History) methods of `replaceState` and `pushState` with `null`. This is done to [support older Microsoft webviews and Office versions](support-ie-11.md). If your add-in relies on these methods and doesn't need to run on Office versions that use the Internet Explorer 11 browser control, replace the Office.js library reference with the following workaround.
27+
28+
```HTML
29+
<script type="text/javascript">
30+
// Cache the history method values.
31+
window._historyCache = {
32+
replaceState: window.history.replaceState,
33+
pushState: window.history.pushState
34+
};
35+
</script>
36+
37+
<script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>
38+
39+
<script type="text/javascript">
40+
// Restore the history method values after loading Office.js
41+
window.history.replaceState = window._historyCache.replaceState;
42+
window.history.pushState = window._historyCache.pushState;
43+
</script>
44+
45+
```
46+
47+
Thank you to [@stepper and the Stack Overflow community](https://stackoverflow.com/questions/42642863/office-js-nullifies-browser-history-functions-breaking-history-usage) for suggesting and verifying this workaround.
48+
2449
## API versioning and backward compatibility
2550

2651
In the previous HTML snippet, the `/1/` in front of `office.js` in the CDN URL specifies the latest incremental release within version 1 of Office.js. Because the Office JavaScript API maintains backward compatibility, the latest release will continue to support API members that were introduced earlier in version 1.

0 commit comments

Comments
 (0)